From 69bc0c7b8d68d5348d51bb334dd5f85f88d711f1 Mon Sep 17 00:00:00 2001
From: dam1n19 <dam1n19@soton.ac.uk>
Date: Thu, 6 Jul 2023 19:19:54 +0100
Subject: [PATCH] Created Seperate Cocotb drivers file and added simple write
 test

---
 flows/makefile.simulate           |  4 +-
 verif/cocotb/adp_cocotb_driver.py | 65 +++++++++++++++++++++++++++
 verif/cocotb/test_adp.py          | 73 ++++++++-----------------------
 3 files changed, 86 insertions(+), 56 deletions(-)
 create mode 100644 verif/cocotb/adp_cocotb_driver.py

diff --git a/flows/makefile.simulate b/flows/makefile.simulate
index 195746d..ec3faef 100644
--- a/flows/makefile.simulate
+++ b/flows/makefile.simulate
@@ -55,6 +55,8 @@ COCOTB_SCRATCH_DIR := $(COCOTB_DIR)/scratch
 # Filelist for Cocotb 
 MAKEFILE_FILELIST     := $(COCOTB_DIR)/makefile.flist
 
+MODULE ?= test_adp
+
 # Create a List of PHONY Targets
 .PHONY: compile_$(SIMULATOR) run_$(SIMULATOR) sim_$(SIMULATOR)
 
@@ -162,7 +164,7 @@ run_cocotb: DEFINES_VC += COCOTB_SIM
 run_cocotb: flist_makefile_nanosoc
 	@mkdir -p $(SIM_DIR)
 	@cd $(SIM_DIR); $(MAKE) -C $(SOCLABS_NANOSOC_TECH_DIR)/verif/cocotb clean SIM_BUILD=$(COCOTB_SCRATCH_DIR)
-	@cd $(SIM_DIR); $(MAKE) -C $(SOCLABS_NANOSOC_TECH_DIR)/verif/cocotb sim SIM=$(COCOTB_SIMULATOR) TESTCASE=$(TESTNAME) GUI=$(GUI) SIM_BUILD=$(COCOTB_SCRATCH_DIR) ACCELERATOR=$(ACCELERATOR)
+	@cd $(SIM_DIR); $(MAKE) -C $(SOCLABS_NANOSOC_TECH_DIR)/verif/cocotb sim MODULE=$(MODULE) SIM=$(COCOTB_SIMULATOR) TESTCASE=$(TESTNAME) GUI=$(GUI) SIM_BUILD=$(COCOTB_SCRATCH_DIR) ACCELERATOR=$(ACCELERATOR)
 
 sim_cocotb: GUI=1
 sim_cocotb: run_cocotb
diff --git a/verif/cocotb/adp_cocotb_driver.py b/verif/cocotb/adp_cocotb_driver.py
new file mode 100644
index 0000000..5397531
--- /dev/null
+++ b/verif/cocotb/adp_cocotb_driver.py
@@ -0,0 +1,65 @@
+#-----------------------------------------------------------------------------
+# SoCLabs ADP Cocotb Drivers
+# Contributors
+#
+# David Mapstone (d.a.mapstone@soton.ac.uk)
+#
+# Copyright 2021-3, SoC Labs (www.soclabs.org)
+#-----------------------------------------------------------------------------
+import cocotb
+
+# Class for ADP AXI Stream Interface
+class ADP():
+    def __init__(self, dut, send, recieve):
+        self.dut      = dut
+        # Send Stream to NanoSoC
+        self.send    = send
+        # Send Recieve to NanoSoC
+        self.recieve = recieve
+        self.monitor_mode = False
+    
+    # Read Block from ADP
+    @cocotb.coroutine
+    async def read(self, length):
+        read_str = ""
+        for i in range(length):
+            data = await self.recieve.read()
+            read_str += chr(data[0])
+        return read_str  
+                
+    # Read ADP String
+    @cocotb.coroutine
+    async def readLine(self):
+        read_str = ""
+        while True:
+            # Read Bytes from ADP AXI Stream
+            data = await self.recieve.read()
+            curr_char = chr(data[0])
+            # Combine Bytes into String
+            if (curr_char != '\n'):
+                read_str += curr_char
+            else:
+                return read_str.strip()
+                              
+    # Write ADP String
+    @cocotb.coroutine
+    async def write(self, string):
+        for i in string:      
+            await self.send.write([ord(i)])
+        await self.send.write([0x0a])
+        read_str = await self.readLine()
+        self.dut.log.info(read_str)
+    
+    # Enter Monitor Mode
+    @cocotb.coroutine
+    async def monitorModeEnter(self):
+        self.dut.log.info("Entering ADP Monitor Mode")
+        await self.send.write([0x1b])
+        self.monitor_mode = True
+        
+    # Exit Monitor Mode
+    @cocotb.coroutine
+    async def monitorModeEnter(self):
+        self.dut.log.info("Exiting ADP Monitor Mode")
+        await self.send.write([0x04])
+        self.monitor_mode = False
\ No newline at end of file
diff --git a/verif/cocotb/test_adp.py b/verif/cocotb/test_adp.py
index b4463d0..f262cec 100644
--- a/verif/cocotb/test_adp.py
+++ b/verif/cocotb/test_adp.py
@@ -1,3 +1,11 @@
+#-----------------------------------------------------------------------------
+# SoCLabs Cocotb ADP Testcases
+# Contributors
+#
+# David Mapstone (d.a.mapstone@soton.ac.uk)
+#
+# Copyright 2021-3, SoC Labs (www.soclabs.org)
+#-----------------------------------------------------------------------------
 from random import randint, randrange, getrandbits, shuffle
 from collections.abc import Iterable
 
@@ -9,52 +17,17 @@ from cocotb.result import TestFailure
 from cocotb.triggers import ClockCycles, Combine, Join, RisingEdge
 
 from cocotbext.axi import AxiStreamBus, AxiStreamSource, AxiStreamSink, AxiStreamMonitor
+from adp_cocotb_driver import ADP
 
 CLK_PERIOD = (10, "ns")
 
-# Class for ADP AXI Stream Interface
-class ADPDriver():
-    def __init__(self, send, recieve):
-        # Send Stream to NanoSoC
-        self.send    = send
-        # Send Recieve to NanoSoC
-        self.recieve = recieve
-    
-    # Read ADP String
-    @cocotb.coroutine
-    async def read(self):
-        read_str = ""
-        while True:
-            # Read Bytes from ADP AXI Stream
-            data = await self.recieve.read()
-            curr_char = chr(data[0])
-            # Combine Bytes into String
-            if (curr_char != '\n'):
-                read_str += curr_char
-            else:
-                return read_str  
-                              
-    # Write ADP String
-    # @cocotb.coroutine
-    # async def read(self):
-    #     read_str = ""
-    #     while True:
-    #         # Read Bytes from ADP AXI Stream
-    #         data = await self.recieve.read()
-    #         curr_char = chr(data[0])
-    #         # Combine Bytes into String
-    #         if (curr_char != '\n'):
-    #             read_str += curr_char
-    #         else:
-    #             return read_str                
-        
-
 # Control ADP AXI Stream bus and create ADP Driver Object
 def setup_adp(dut):
     adp_sender = AxiStreamSource(AxiStreamBus.from_prefix(dut, "txd8"), dut.XTAL1, dut.NRST)
     adp_reciever = AxiStreamSink(AxiStreamBus.from_prefix(dut, "rxd8"), dut.XTAL1, dut.NRST)
     logging.getLogger("cocotb.nanosoc_tb.rxd8").setLevel(logging.WARNING)
-    driver = ADPDriver(adp_sender, adp_reciever)
+    logging.getLogger("cocotb.nanosoc_tb.txd8").setLevel(logging.WARNING)
+    driver = ADP(dut, adp_sender, adp_reciever)
     return driver
 
 # Start Clocks and Reset System
@@ -66,28 +39,12 @@ async def setup_dut(dut):
     dut.NRST.value = 1
     await ClockCycles(dut.XTAL1, 2)
 
-@cocotb.coroutine
-async def write(driver, buf):
-    wr_count = 0
-    for i in buf:
-        print(i)
-        await driver.tx.send(str(ord(i)))
-    return wr_count
-    
-@cocotb.coroutine
-async def write(driver, buf):
-    wr_count = 0
-    for i in buf:
-        print(i)
-        await driver.tx.send(str(ord(i)))
-    return wr_count
-
 # Wait for bootcode to finish
 @cocotb.coroutine
 async def wait_bootcode(dut, driver):
     bootcode_last = "** Remap->IMEM0"
     while True:
-        read_str = await driver.read()
+        read_str = await driver.readLine()
         dut.log.info(read_str)
         if read_str == bootcode_last:
             break
@@ -118,6 +75,12 @@ async def test_adp_write(dut):
     dut.log.info("Setup Complete")
     dut.log.info("Starting Test")
     await wait_bootcode(dut, adp_driver)
+    dut.log.info("Bootcode Finished")
+    await adp_driver.monitorModeEnter()
+    await adp_driver.write('A 0x10000000\n')
+    await adp_driver.write('R 4\n')
+    for i in range(4):
+        dut.log.info(await adp_driver.readLine())
     dut.log.info("ADP Write Test Complete")
 
 
-- 
GitLab