Last active
August 29, 2015 14:22
-
-
Save darius/a406529efa563f3b75f5 to your computer and use it in GitHub Desktop.
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
""" | |
machine = assignment*. | |
assignment = lvalue _? '=' _? expr _? '\n'. | |
expr = lvalue _ lvalue _ lvalue. | |
lvalue = name ('.' literal)*. | |
literal = '0' | '1' | name. | |
name = /[^.]+/. | |
_ = /\s+/. | |
comment = /#.*/. | |
""" | |
def eval_hdl(text, env): | |
"""`text` is a machine description following the grammar above. | |
`env` is a dict, shared for two purposes: | |
* sequential-circuit state (and RAM) | |
* I/O devices | |
(It's also used as a scratchpad for combinational-logic state; | |
no separation of that state is enforced here.) | |
To use this for a particular machine, call eval_hdl() in a loop, | |
once for each most-basic machine cycle, reading/writing the data | |
of any special devices from/to env.""" | |
env['0'] = 0 | |
env['1'] = 1 | |
def key(lvalue): | |
if '.' not in lvalue: return lvalue | |
name, subscrs = lvalue.split('.', 1) | |
return (name,) + tuple(env.get(s, 0) for s in subscrs) | |
for line in text.splitlines(): | |
if '#' in line: line = line[:line.index('#')] | |
if not line: continue | |
dest, eq, test, if0, if1 = line.split() | |
assert eq == '=' | |
env[key(dest)] = env.get(key(if1 if env.get(key(test)) else if0), | |
0) | |
""" | |
Some lacks that occur to me: | |
* Assertions (to help future implementors get it right) | |
* Some way to reduce the verbosity of: | |
* ROM tables | |
* bit-parallel logic | |
* Maybe a syntactic distinction between devices/memory and combinational logic? | |
* Like device names start with '%'? Though nothing's stopping you from writing that way. | |
Adding a way to define and access ROM tables would let us dispense with the | |
built-in MUX gate (implemented on the last line). You'd just define whatever | |
gates you want, as look-up tables. | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Let's consider another design with ROM tables and assertions. Example program: