62 lines
1.4 KiB
Python
62 lines
1.4 KiB
Python
import cocotb
|
|
from cocotb.handle import Immediate
|
|
|
|
from cocotb.clock import Clock
|
|
from cocotb.triggers import Timer, RisingEdge, FallingEdge
|
|
|
|
from collections import defaultdict
|
|
|
|
import struct
|
|
import random
|
|
|
|
import os
|
|
|
|
CLK_PERIOD = 5
|
|
|
|
memory = defaultdict(int)
|
|
|
|
def write_dword(addr: int, data: int):
|
|
memory[addr + 0] = (data >> 0) & 0xff
|
|
memory[addr + 1] = (data >> 8) & 0xff
|
|
memory[addr + 2] = (data >> 16) & 0xff
|
|
memory[addr + 3] = (data >> 24) & 0xff
|
|
|
|
def write_byte(addr: int, data: int):
|
|
memory[addr] = data & 0xff
|
|
|
|
def write_bytes(addr: int, data: bytes| list[int]):
|
|
for i, val in enumerate(data):
|
|
memory[addr + i] = int(val)
|
|
|
|
async def handle_memory(dut):
|
|
while True:
|
|
await RisingEdge(dut.clk)
|
|
addr = int(dut.AB.value)
|
|
we = bool(dut.WE.value)
|
|
|
|
dut.DI.value = memory[addr]
|
|
|
|
if we:
|
|
memory[addr] = int(dut.DO.value)
|
|
|
|
@cocotb.test
|
|
async def test_lda(dut):
|
|
cocotb.start_soon(Clock(dut.clk, CLK_PERIOD, unit="ns").start())
|
|
cocotb.start_soon(handle_memory(dut))
|
|
|
|
path = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
base_addr = 0xfffff000
|
|
|
|
with open(f"{path}/asm_source/lda_test", "rb") as file:
|
|
for i, val in enumerate(file.read()):
|
|
write_byte(base_addr+i, val)
|
|
|
|
dut.RDY.value = Immediate(1)
|
|
|
|
dut.reset.value = Immediate(1)
|
|
for _ in range(10):
|
|
await RisingEdge(dut.clk)
|
|
dut.reset.value = 0
|
|
|
|
await FallingEdge(dut.RDY_O) |