OR-1 dataflow CPU sketch
at main 91 lines 2.3 kB view raw
1"""Built-in macro library for dfasm. 2 3These macros are automatically available in all dfasm programs. 4The BUILTIN_MACROS string is prepended to user source before parsing. 5 6Macro parameters can appear in edge endpoints via ${param} syntax, 7in opcode position via ${op}, and in qualifier positions via |${pe} 8and :${port}. The expand pass resolves all ParamRef placeholders. 9 10Macros use @ret/@ret_name markers for output wiring via the |> syntax 11at call sites. 12""" 13 14BUILTIN_MACROS = """\ 15; === Built-in Macro Library === 16; These macros are automatically available in all dfasm programs. 17 18; --- Counted loop --- 19; Topology: counter -> compare -> body_fan (fanout) -> inc -> feedback 20; compare L -> body_fan (pass as fanout) -> @ret_body + &inc 21; compare R -> @ret_exit 22; Call with: #loop_counted &init, &limit |> body=&process, exit=&done 23#loop_counted init, limit |> { 24 &counter <| add 25 &compare <| brgt 26 &counter |> &compare:L 27 &body_fan <| pass 28 &compare |> &body_fan:L 29 &inc <| inc 30 &body_fan |> &inc:L 31 &inc |> &counter:R 32 ${init} |> &counter:L 33 ${limit} |> &compare:R 34 &body_fan |> @ret_body 35 &compare |> @ret_exit:R 36} 37 38; --- Condition-tested loop --- 39; Topology: &gate (gate) -> L=body, R=exit 40; Call with: #loop_while &test_src |> body=&process, exit=&done 41#loop_while test |> { 42 &gate <| gate 43 ${test} |> &gate:L 44 &gate |> @ret_body 45 &gate |> @ret_exit:R 46} 47 48; --- Permit injection (variadic) --- 49; Injects one const(1) seed token per target. 50; Call with: #permit_inject &gate_a, &gate_b, &gate_c |> @a, @b, @c 51#permit_inject *targets |> { 52 $( 53 &p <| const, 1 54 ${targets} |> &p 55 &p |> @ret 56 ),* 57} 58 59; --- Binary reduction trees (parameterized opcode) --- 60; Call with: #reduce_2 add |> &result_node 61#reduce_2 op |> { 62 &r <| ${op} 63 &r |> @ret 64} 65 66; Call with: #reduce_3 sub |> &result_node 67#reduce_3 op |> { 68 &r0 <| ${op} 69 &r1 <| ${op} 70 &r0 |> &r1:L 71 &r1 |> @ret 72} 73 74; Call with: #reduce_4 add |> &result_node 75#reduce_4 op |> { 76 &r0 <| ${op} 77 &r1 <| ${op} 78 &r2 <| ${op} 79 &r0 |> &r2:L 80 &r1 |> &r2:R 81 &r2 |> @ret 82} 83 84""" 85 86# Count newlines in BUILTIN_MACROS for line number offset calculation 87_BUILTIN_LINE_COUNT: int = BUILTIN_MACROS.count("\n") 88 89__all__ = [ 90 "BUILTIN_MACROS", 91]