Skip to content

Instantly share code, notes, and snippets.

@urish
Created July 11, 2024 06:34
Show Gist options
  • Save urish/3e9b6ec7439516778c8be94421f3374a to your computer and use it in GitHub Desktop.
Save urish/3e9b6ec7439516778c8be94421f3374a to your computer and use it in GitHub Desktop.
tt05_dffram_test.py
# SPDX-License-Identifier: Apache-2.0
# Copyright (C) 2024, Uri Shaked
from ttboard.demoboard import DemoBoard
from ttboard.mode import RPMode
import random
from machine import Pin
WRITE_EN = 0x80
class DFFRAM:
def __init__(self, tt):
self.tt = tt
tt.shuttle.tt_um_urish_dffram.enable()
tt.bidir_mode = [Pin.OUT] * 8
tt.clock_project_stop()
tt.project_clk(False)
tt.reset_project(True)
tt.clock_project_once()
tt.reset_project(False)
def write(self, addr, value):
self.tt.bidir_byte = value
self.tt.input_byte = (addr & 0x7F) | WRITE_EN
self.tt.clock_project_once() # Write happens on the falling edge of the clock?
self.tt.project_clk(True)
self.tt.input_byte = 0 # Disable write
self.tt.project_clk(False)
def read(self, addr):
self.tt.input_byte = addr
self.tt.clock_project_once()
return self.tt.output_byte
def shuffle(array):
"""Shuffle an array in place"""
for i in range(len(array) - 1, 0, -1):
j = random.randrange(i + 1)
array[i], array[j] = array[j], array[i]
return array
def run():
tt = DemoBoard.get()
tt.mode = RPMode.ASIC_RP_CONTROL
dffram = DFFRAM(tt)
print("Writing 128 bytes")
for i in range(128):
dffram.write(i, i ^ 0x42)
print("Verifying 128 bytes")
for i in range(128):
assert dffram.read(i) == i ^ 0x42
print("Writing 128 random bytes")
random_bytes = [random.randint(0, 255) for _ in range(128)]
random_offsets = shuffle(list(range(128)))
for i in random_offsets:
dffram.write(i, random_bytes[i])
print("Verifying 128 random bytes")
for i in range(128):
assert dffram.read(i) == random_bytes[i]
print("All tests passed")
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment