Created
September 26, 2011 10:05
-
-
Save buhii/1241967 to your computer and use it in GitHub Desktop.
Waveform example with NodeBox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GRID = (40, 40) | |
BUS_STRIPE = 10 | |
class Wave(object): | |
@classmethod | |
def clk(cls, num): | |
ret = [] | |
for i in range(num): | |
ret.append((i + 1) % 2) | |
return ret | |
def __init__(self, name, data, bit_range=1, domain=False): | |
self.name = name | |
self.data = data | |
self.bit_range = bit_range | |
self.domain = domain | |
def draw_bit(self): | |
last_data = self.data[0] | |
l = self.domain if self.domain else GRID[0] | |
for i, x in enumerate(self.data): | |
if last_data != x: | |
line(i * l, 0, i * l, GRID[1]) | |
if x in (0, "0", "_"): | |
line(i * l, GRID[1], (i + 1) * l, GRID[1]) | |
else: | |
line(i * l, 0, (i + 1) * l, 0) | |
last_data = x | |
def draw_bus(self): | |
last_data = self.data[0] | |
l = self.domain if self.domain else GRID[0] | |
for i, x in enumerate(self.data): | |
if last_data != x: | |
line(i * l, 0, i * l + BUS_STRIPE, GRID[1]) | |
line(i * l, GRID[1], i * l+ BUS_STRIPE, 0) | |
line(i * l + BUS_STRIPE, 0, (i + 1) * l, 0) | |
line(i * l + BUS_STRIPE, GRID[1], (i + 1) * l, GRID[1]) | |
else: | |
line(i * l, 0, (i + 1) * l, 0) | |
line(i * l, GRID[1], (i + 1) * l, GRID[1]) | |
if last_data != x or i == 0: | |
text(x, i * l + GRID[0] / 2, 25) | |
last_data = x | |
def draw(self): | |
if not self.data: return | |
if self.bit_range == 1 and not (set(self.data) - set([0, 1, '0', '1', '_', '-', '^'])): | |
self.draw_bit() | |
else: | |
self.draw_bus() | |
def draw(*waves, **kwargs): | |
# get max label's width | |
label_width = 0 | |
for wave in waves: | |
label_width = max(label_width, textwidth(wave.name)) | |
label_width += 20 | |
# draw waves | |
for i, wave in enumerate(waves): | |
text(wave.name, 5, 25) | |
translate(label_width, 0) | |
wave.draw() | |
translate(-label_width, GRID[1] * 2) | |
# draw grids | |
if 'width' in kwargs: | |
stroke(0.8) | |
strokewidth(1) | |
translate(label_width, -GRID[1] * 2 * len(waves)) | |
for i in range(0, 30, kwargs['width']): | |
line(i * GRID[0], 0, i * GRID[0], len(waves) * 2 * GRID[1]) | |
# main | |
size(1024, 400) | |
print "processing..." | |
fill(0.2) | |
stroke(0.2) | |
strokewidth(1) | |
font("Helvetica", 13) | |
translate(10, 20) | |
draw( | |
Wave("CLK", Wave.clk(36), domain=20), | |
Wave("TPI_DATA", | |
"000000001111100000"), | |
Wave("TPI_CYCLE", | |
"000000000111110000"), | |
Wave("TPI_COUNT", | |
"000000000012345555"), | |
width=2) | |
print "completed" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment