Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests for converting model with dynamically generated ast #170

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions pymtl/tools/translation/verilog_from_ast_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#=======================================================================
# verilog_from_ast_test.py
#=======================================================================

import pytest
import random
from ast import *

from pymtl import *
from pclib.test import run_test_vector_sim
from verilator_sim import TranslationTool

pytestmark = requires_verilator

class ASTRTLModel(Model):
def __init__( s ):
s.a = InPort(2)
s.b = InPort(2)

s.out = OutPort(2)

# a simple clocked adder
# @s.posedge_clk
# def logic():
# s.out.next = s.a + s.b

# generate the model from ast
tree = Module(body=[
FunctionDef(name='logic', args=arguments(args=[], defaults=[]),
body= [
Assign(targets=[
Attribute(value=Attribute(value=Name(id='s', ctx=Load()), attr='out', ctx=Load()), attr='next', ctx=Store())
],
value=BinOp(left=Attribute(value=Name(id='s', ctx=Load()), attr='a', ctx=Load()), op=Add(), right=Attribute(value=Name(id='s', ctx=Load()), attr='b', ctx=Load()))
)
],
decorator_list=[
Attribute(value=Name(id='s', ctx=Load()), attr='posedge_clk', ctx=Load())
],
returns=None)
])
tree = fix_missing_locations(tree)
exec(compile(tree, filename='<ast>', mode='exec'))

def test_ast_rtl_model_works_in_simulation():
mod = ASTRTLModel()

test_vector_table = [('a', 'b', 'out*')]
last_result = '?'
for i in xrange(3):
rv1 = Bits(2, random.randint(0, 3))
rv2 = Bits(2, random.randint(0, 3))
test_vector_table.append( [ rv1, rv2, last_result ] )
last_result = Bits(2, rv1 + rv2)

run_test_vector_sim(mod, test_vector_table)

def test_ast_rtl_model_to_verilog():
mod = ASTRTLModel()
# TranslationTool should successfully compile ASTRTLModel
tool = TranslationTool(mod)