-
Hi. Is it possible to have a circuit that doesn't use any registers in bsv? Like a 2 bit adder? Or would all circuits end up having registers? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
Certainly, you can express arbitrary combinational circuits in BSV. Broadly speaking, any BSV expression with a "value type" (not involving Action or ActionValue types) represents a combinational circuit. For example, given two 1-bit signals 'a' and 'b', the expression
represents a half-adder with the sum and carry bits. Any expression can be encapsulated into a function, e.g.,
The following is a full adder, which also takes an carry-in bit argument:
With function composition and loops around such expressions, you can build this up into a full N-bit ripple-carry adder. With recursive functions around such expressions, you can write more efficient adders like Wallace Tree Adders (look for 'Wallace.bs' in the bsc libraries). And they can be polymporphic in N, the bit-widths of the inputs and outputs. See 'polymorpic types' in the manuals and tutorials. And, finally, methods of modules can be purely combinational, so combinational circuits can be encapsulated in modules. For standard arithmetic/logic operations, we typically don't build circuits up from scratch with AND/OR/NOT gates (unless one wants a specialist circuit like a Wallace adder); BSV provides a variety of arithmetic-logic operators addition, multiplication, AND, OR, XOR, arithmetic and logical shifts etc. and we leave it to Verilog/Synthesis tool to synthesize the details of those. Conditional expressions represent multiplexers, and can be written in several ways (here 'a' and 'b' can be arbitrary expression but must have the same type):
|
Beta Was this translation helpful? Give feedback.
-
Thanks for the detailed reply with examples. Was very helpful. I made this as an attempt at a 4b ripple carry adder:
How can we write a test bench for it? I got to first have module here, right? I tried:
I had a glimpse at tutorial, but still wasn't sure. A register won't be needed in the code for the adder itself, right? Only in the test bench? |
Beta Was this translation helpful? Give feedback.
-
I have attached a tar file with some discussion and examples: tmp.tar.gz The following text repeats the README in the tar file, for convenience. // ================================================================
Actually (congratulations!) your solution is more general, it's a // ================================================================ Your code in rca.bsv is functionally correct, although when we compile
It takes some analysis to realise that, no, you're not using any The solution is simple: appease 'bsc' by intializing 'cs'. Change:
to
and then it will compile without complaint. This small modification is in file 'rca1.bsv'. // ================================================================ Although perhaps overkill for this small example, file 'rca2.bsv' // ================================================================
No, the adder is fine, as is.
Depends on what you want to do. The file 'Top1.bsv' shows a simple testbench with no registers; its Since a pure function like 'rca' can be invoked any number of times in The file 'Top3.bsv' uses a register to generate the 16x16 input The BSV libraries also contain pseudo-random-number generator modules // ================================================================ In 'src_BSV',
In the top-level dir, See 'transcript_make_b_all_Top2_rca1.txt' for an example transcript. // ================================================================ |
Beta Was this translation helpful? Give feedback.
-
Thanks very much for taking the time to make the examples! The comments in the source files helped a lot. Like this one in Top2.bsv:
I would've missed it otherwise.
Earlier, I had been trying to figure out a way to make
That certainly feels better. Had an initial trouble understanding what (The type looks more readable in haskell.)
So, it would be like:
Is it? Regarding using Would using vectors instead of loops still result in the same circuit? They are merely containers? I couldn't make it spit out verilog files. Probably because rca.bsv don't have any modules. And thanks for the Makefile. It looks as if I can pull bits and pieces from it to make one of my own. 🙂 |
Beta Was this translation helpful? Give feedback.
Certainly, you can express arbitrary combinational circuits in BSV.
Broadly speaking, any BSV expression with a "value type" (not involving Action or ActionValue types) represents a combinational circuit. For example, given two 1-bit signals 'a' and 'b', the expression
represents a half-adder with the sum and carry bits. Any expression can be encapsulated into a function, e.g.,
The following is a full adder, which also takes an carry-in bit argument: