cmtrs 0.1.2

A rule-based embedded HDL in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//! `cmtrs` is an embedded domain-specific language in Rust for digital chip design.
//!
//! `cmtrs` provides:
//! + Rule-based RTL hardware description
//! + Port connections with methods
//! + Multi-cycle cycle-accurate high level control statements to simplify finite state machines (FSMs)
//! + Embedded hardware generators for complex parameterization
//!
//! # `cmtrs` Basics
//!
//! ## VERY IMPORTANT!!!
//!
//! `cmtrs` use nightly features for span support. You need add a file
//! `.cargo/config.toml` with the following content: 
//! 
//! ```toml
//! [build]
//! rustflags = "--cfg procmacro2_semver_exempt"
//! ```
//! 
//! as well as a `rust-toolchain.toml` file with the following content:
//! 
//! ```toml
//! [toolchain]
//! channel = "nightly-2024-12-25"
//! ```
//! 
//! ## Interface declaration
//!
//! An interface is how a module looks like from the outside, which describes its type parameters, IO ports and methods.
//!
//! Modules with the same interface will have the same Rust type in `cmtrs`, which means different implementation can be selected by during generation.
//! From this point of view, interfaces are similar to traits.
//!
//! Module methods will become real Rust methods after instantiation, which can be invoked to trigger certain behavior of the module.
//! IO ports will be automatically connected according to method invocations.
//!
//! ### Example
//!
//! ```
//! use cmtrs::*;
//! itfc_declare!(
//!   param T; // Type parameter
//!   // IO declaration
//!   struct Counter { // Name of the interface
//!     set_val: input param T, // input port
//!     count: output param T,  // output port
//!   }
//!   // methods declaration
//!   method read() -> (count);  // output to 'count' port
//!   method set(set_val) ;      // input from 'set_val' port
//! );
//! ```
//!
//! ## Module generation
//!
//! To implement a module of certain interface, one must write a generator function, which generates hardware through function execution.
//!
//! A generator function is a function with `#[module]` attribute and returns a module interface.
//! The `#[module]` attribute is a proc-macro that will record all hardware behaviors inside of the function to produce a module.
//!
//! ### Example
//!
//! ```
//! # use cmtrs::*;
//! # itfc_declare!(
//! #   param T; // Type parameter
//! #   // IO declaration
//! #   struct Counter { // Name of the interface
//! #     set_val: input param T, // input port
//! #     count: output param T,  // output port
//! #   }
//! #   // methods declaration
//! #   method read() -> (count);  // output to 'count' port
//! #   method set(set_val) ;      // input from 'set_val' port
//! # );
//!
//! #[module]
//! fn make_counter(lim: usize, ty: &Type) -> Counter {
//!   // Provide type parameters and create ios.
//!   let io = io! {
//!     T: ty
//!   };
//!
//!   // Instantiate a register sub-module
//!   // `mut` here is required for the %= operator, though it is not mutable in-fact
//!   // `stl::reg` is the generator function of reg
//!   let mut reg_i = instance!(stl::reg(ty));
//!
//!   // an always rule will be automatically fired if the conditions are met
//!   // increase reg_i if i < lim
//!   let inc = always! {
//!     [reg_i.lt(lim.lit(ty))] // guard expression
//!     () { // ios
//!       reg_i %= &reg_i + 1.lit(ty); // body
//!       // equals reg_i.write(reg_i.read() + 1.lit(ty))
//!     }
//!   };
//!
//!   // reset reg_i to 0 if i>=lim
//!   let rst = always! {
//!     [reg_i.ge(lim.lit(ty))]
//!     () {
//!       reg_i %= 0.lit(ty);
//!     }
//!   };
//!
//!   // a method will be fired if it is invoke and conditions are met
//!   // set reg_i to the input `set_val`
//!   let set = method!(
//!     (io.set_val) {
//!       reg_i %= io.set_val;
//!     }
//!   );
//!
//!   // read the value of reg_i to the output `count`
//!   let read = method! {
//!     () -> (io.count) {
//!       ret!(&reg_i)
//!     }
//!   };
//!
//!   // `set` method has confict with itself,
//!   // so only one `set` can be called in one cycle
//!   method_rel!(set C set);
//!    
//!   // combinational order of the rules and methods in one cycle
//!   // rules
//!   schedule!(read, set, inc, rst);
//!
//!   // Automatically returns the generated module
//!   // Do not try to return anything here
//! }
//! ```
//!
//! ## List of commonly used macros in module generation
//!
//! + [`itfc_declare!`] Declares an interface.
//! + [`#[module]`](`module`) Make a module generator.
//! + [`io!`] Provides type parameters and sub-interface instances to the module, returns a struct containing all ios and sub-interfaces.
//! + [`anno!`] Give annotations to the module.
//! + [`always!`] Declare a always rule.
//! + [`method!`] Declare a method rule.
//! + [`ext_method!`] Declare a external method.
//! + [`method_rel!`] Declare the relationship between rules and methods.
//! + [`schedule!`] Declare a timing order for the rules and methods within one cycle.
//! + [`var!`] Make a single assign multiple use variable (wire) that can be used within a rule/method.
//! + [`if_!`] Combinational condition.
//! + [`ret!`] Return a value to statements at higher-level.
//! + [`statement!`] Make an expression into a statement.
//! + [`#[gen_fn]`](`gen_fn`) Make a generate function, which records macros calls and can be transferred back to its caller.
//! + [`generate!`] Actually generate the things into the module returned by a generate function.
//! + [`move_!`] Use on `move ||` closures so that macros can be called inside of it.
//! + [`sim_exit!`] Declare the finish of simulation.
//! + [`sim_print!`] Print values during simulation.
//!
//! ## High-level control primitives
//!
//! High level control primitives describes hardware behaviors that takes multiple cycles to finish. Currently we provide a sub-set of C-like primitives.
//!
//! + [`step!`] Group operations into an atomic step controlled by FSMs.
//! + [`seq!`] Execute children statements in sequential cycles.
//! + [`par!`] Execute children statements in parallel, then join them.
//! + [`branch!`] Make a fork on the FSM, decide which statement to execute in the next cycle.
//! + [`for_!`] Execute statements in C-like for loop.
//!
//! High-level primitives can only be used in rules with `fsm` or `pipeline` keyword. Such rule will become stateful and will be executed through multiple cycles once fired.
//! `fsm` rules are similar to a process, which takes inputs, does computation in the next cycles, and returns the result.
//! `pipeline` rules are similar to coroutines, which can be repeatedly invoked for multiple inputs and outputs.
//! Each statement at top-level in a pipeline works concurrently, and will be fired once the previous stage is finish.
//!
//! ### Example
//!
//! The Collatz Conjucture is a sequence, where `a[i+1] = a[i]/2` if a[i] is even, otherwise `a[i+1]=a[i]*3+1`.
//! The following example wil calculate such sequence.
//!
//! ```
//! use cmtrs::*;
//!
//! itfc_declare! {
//!   struct Collatz {
//!     in_: input Type::UInt(8),
//!     out: output Type::UInt(8)
//!   }
//!   method out () -> (out);
//!   method start (in_);
//! }
//!
//! #[module]
//! fn make_collatz() -> Collatz {
//!   let io = io! {};
//!
//!   let t = Type::UInt(8);
//!   let r = instance!(stl::reg(&t));
//!
//!   let out = method!(
//!     () -> (io.out) { ret!(r.read()) }
//!   );
//!
//!   let start = method!(
//!     fsm; // the fsm keyword
//!     (io.in_) {
//!       for_!{(r.write(io.in_);true; ; r.read().gt(literal(1, &t))) {
//!         branch!{r.read() & literal(1, &t) { // odd
//!           branch!{r.read().ne(1.lit(&t)){ // not one
//!             seq!{
//!               step!{ r.write(r.read() * literal(3, &t)); };
//!               step!{ r.write(r.read() + literal(1, &t)); };
//!             }
//!           } }
//!         } else { // even
//!           step!{ r.write(r.read() >> 1); };
//!         }
//!         };
//!       } }
//!     }
//!   );
//! }
//! ```
//!
//! ## Nested Interface
//!
//! An interface can be part of the interface of the parent module. This is useful for modules like FIFO or memory.
//! Within the module generator, nested interfaces can be accessed from `io!{}`.
//! From the outside, nested interfaces are public members of the Rust type of the module instance.
//!
//! ### Example
//!
//! The sequence merge example merges two sequence from FIFOs, and put them into the output FIFO.
//!
//! ```
//! use cmtrs::*;
//!
//! itfc_declare! {
//!   param T;
//!   struct SeqMerge {
//!     // a is a nested itfc, of type FIFO,
//!     // with parameter T, equal to the parameter T of SeqMerge
//!     a: itfc stl::FIFO{T: param T},
//!     b: itfc stl::FIFO{T: param T},
//!     c: itfc stl::FIFO{T: param T},
//!   }
//! }
//!
//! #[module]
//! fn seq_merge(t: &Type) -> SeqMerge {
//!   let io = io! {
//!     T: t,
//!     // use io macro to provide the exact instances to the nested itfc
//!     a: stl::fifo_default(4, t),
//!     b: stl::fifo_default(4, t),
//!     c: stl::fifo_default(4, t)
//!   };
//!   anno!("synthesis":"true");
//!
//!   let reg_a = instance!(stl::Reg::new(t));
//!   let reg_b = instance!(stl::Reg::new(t));
//!
//!   let reg_a_valid = instance!(stl::Reg::new(&Type::UInt(1)));
//!   let reg_b_valid = instance!(stl::Reg::new(&Type::UInt(1)));
//!
//!   let in_a = always!(
//!     [!reg_a_valid.read()]
//!     () {
//!       // use nested itfc from the io
//!       reg_a.write(io.a.deq());
//!       reg_a_valid.write(true);
//!     }
//!   );
//!   let in_b = always!(
//!     [!reg_b_valid.read()]
//!     () {
//!       reg_b.write(io.b.deq());
//!       reg_b_valid.write(true);
//!     }
//!   );
//!
//!   let out = always! {
//!     [reg_a_valid.read() & reg_b_valid.read() & !io.c.full()]
//!     () {
//!        let a = var!(reg_a.read());
//!        let b = var!(reg_b.read());
//!        if_!{(&a).le(&b) {
//!          io.c.enq(a);
//!          reg_a_valid.write(false);
//!        } else {
//!          io.c.enq(b);
//!          reg_b_valid.write(false);
//!        } }
//!      }
//!   };
//!
//!   schedule!(out, in_a, in_b);
//! }
//!
//! itfc_declare!(
//!   param T;
//!   struct Top {
//!     in_a: input param T,
//!     in_b: input param T,
//!     out_c: output param T,
//!   };
//!   method input_a(in_a);
//!   method input_b(in_b);
//!   method output_c()->(out_c);
//! );
//!
//! #[module]
//! fn make_top(t: &Type) -> Top {
//!   let io = io! {T: t};
//!   anno!("synthesis":"true");
//!
//!   let merger = instance!(seq_merge(t));
//!
//!   let input_a = method! {
//!     [!merger.a.full()]
//!     (io.in_a) {
//!       // use nested itfc from the outside
//!       merger.a.enq(io.in_a);
//!     }
//!   };
//!
//!   let input_b = method! {
//!     [!merger.b.full()]
//!     (io.in_b) {
//!       merger.b.enq(io.in_b);
//!     }
//!   };
//!
//!   let output_c = method! {
//!     () -> (io.out_c) {
//!       merger.c.deq()
//!     }
//!   };
//!
//!   schedule!(input_a, input_b, output_c);
//! }
//! ```
//!
//! ## Macros for dynamic generation
//!
//! Module can be completely dynamically generated, with arbitary IOs and methods. The following macros are helpful for this purpose.
//!
//! + [`named_always!`]
//! + [`named_method!`]
//! + [`named_ext_method!`]
//! + [`named_var!`]
//! + [`input!`]
//! + [`output!`]
//! + [`set_name!`]
//! + [`method_rel_raw!`]
//! + [`schedule_raw!`]
//! + [`ret_raw!`]
//!
//! ## Type System
//!
//! Currently `cmtrs` use completely dynamic types. See [`Type`] for more information.
//!
//! ## Operators
//!
//! Currenly `cmtrs` support a limitted range of operators. See [`ops`] for more information.
//!
//! ## Standard library
//!
//! `cmtrs` provide some standard templates in hardware, including [`Wire`](stl::Wire), [`Reg`](stl::Reg), [`FIFO`](stl::FIFO) and memories.
//! See [`stl`] for more information.
//!
//! ## Elaboration and Simumlation
//!
//! Elaboration and Simulation depends on crate [`cmtc`]. `cmtrs` can be elaborated into FIRRTL or System Verilog.
//! We also support writing testbenches in `cmtrs`, which will be transformed into C testbench for Verilator or Khronos.
//!
//! ### Elaboration
//!
//! ```
//! use cmtrs::*;
//! use cmtc::*;
//! # itfc_declare!(struct SomeModule{});
//! # #[module] fn make_module() -> SomeModule {io!{}};
//! // To System Verilog
//! let module = make_module();
//! elaborate(module, sv_config("path/to/sv.sv")).unwrap();
//! // To FIRRTL
//! let module = make_module();
//! elaborate(module, fir_config("path/to/fir.fir")).unwrap();
//! ```
//!
//! ### Testbench
//!
//! A `cmtrs` testbench is a non-synthesizable top-module.
//! It can use anything in normal modules, and additonally can use simulation only functionalities like [`Interger`](stl::Integer) and [`sim_print`].
//!
//! ```ignore
//! use cmtrs::*;
//! use cmtc::*;
//!
//! # itfc_declare!(param T; struct Counter{out: output param T} method read() -> (out););
//! # #[module] fn make_counter(n: usize, ty: &Type) -> Counter { io!{T: ty}; }
//!
//! itfc_declare!(
//!   struct Tb {}
//! );
//!
//! #[module]
//! fn make_tb() -> Tb {
//!   io! {}
//!   anno!("is_tb": "true");
//!
//!   let counter = instance!(make_counter(10, &Type::UInt(4)));
//!   let mut cycle = instance!(stl::integer());
//!
//!   let sim = always!(
//!     [cycle.lt(stl::int(20))]
//!     () {
//!       cycle %= &cycle + stl::int(1);
//!       sim_print!("cycle: ", cycle, "count: ", counter.read());
//!     }
//!   );
//!
//!   let exit = always!(
//!     [cycle.ge(stl::int(20))]
//!     () {
//!       sim_exit!();
//!     }
//!   );
//!
//!   method_rel!(sim CF exit);
//! }
//!
//! // Make Verilator workspace
//! let tb = make_tb();
//! elaborate(tb, verilator_config("path/to/dir")).unwrap();
//!
//! // Make Khronos workspace
//! let tb = make_tb();
//! elaborate(tb, ksim_config("path/to/dir")).unwrap();
//! ```
//!
//! The generated Veriloator workspace is configured with CMake. The executable is named `Vsim`.
//! ```bash
//! cd path/to/dir
//! mkdir build
//! cd build
//! cmake -GNinja ..
//! ninja
//! ./Vsim
//! ```
//!
//! The generated Khonos workspace is configured with MakeFile. The executable have the same name as the top module.
//! ```bash
//! cd path/to/dir
//! make all
//! ./name_of_top_module
//! ```
//!
pub mod gen_ir;
pub mod interface;
pub mod stl;
pub mod type_sys;

pub use std::panic::Location;

use cmtir as ir;
pub use cmtir::{utils, IRDump, MethodRel, MySpan, PrintItem, RuleSignature, RuleTiming};
pub use cmtrs_macros::*;
pub use interface::*;
// pub use paste::paste;
pub use type_sys::Type;

pub fn extract_span_from_location(location: &'static Location<'static>) -> MySpan {
  let file = location.file();
  let start_line = location.line() as usize;
  let start_column = location.column() as usize;
  let end_line = location.line() as usize;
  let end_column = location.column() as usize;
  let start_byte = 0;
  let end_byte = 0;
  MySpan {
    file: file.to_string(),
    start_line,
    start_column,
    end_line,
    end_column,
    start_byte,
    end_byte,
  }
}