Skip to content
Closed
Show file tree
Hide file tree
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
11 changes: 9 additions & 2 deletions 0_demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"\n",
"Take the following example of a 3-point moving average implemented in the style of a FIR filter.\n",
"\n",
"<img src=\"https://raw.githubusercontent.com/freechipsproject/chisel3/master/doc/images/fir_filter.svg?sanitize=true\" width=\"512\" />\n",
"<img src=\"https://raw.githubusercontent.com/chipsalliance/chisel3/e11d8b85ab2d37c1fbfda6a752c7c920fb7269ac/docs/src/images/fir_filter.svg\" width=\"512\" />\n",
"\n",
"Chisel provides similar base primitives as synthesizable Verilog and *can* be used as such! Run next cell to declare our Chisel module."
]
Expand Down Expand Up @@ -207,6 +207,13 @@
"\n",
"[Return to the top.](#top)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -222,7 +229,7 @@
"mimetype": "text/x-scala",
"name": "scala",
"nbconvert_exporter": "script",
"version": "2.12.8"
"version": "2.12.10"
}
},
"nbformat": 4,
Expand Down
139 changes: 71 additions & 68 deletions 2.1_first_module.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
cleaclear "execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"val path = System.getProperty(\"user.dir\") + \"/source/load-ivy.sc\"\n",
Expand All @@ -51,14 +49,14 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"import chisel3._\n",
"import chisel3.util._\n",
"import chisel3.iotesters.{ChiselFlatSpec, Driver, PeekPokeTester}"
"import chisel3.tester._\n",
"import chisel3.tester.RawTester.test\n",
"import dotvisualizer._"
]
},
{
Expand All @@ -76,9 +74,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"// Chisel Code: Declare a new module definition\n",
Expand Down Expand Up @@ -131,9 +127,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"// Scala Code: Elaborate our Chisel design by translating it to Verilog\n",
Expand All @@ -154,9 +148,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"// Chisel Code, but pass in a parameter to set widths of ports\n",
Expand Down Expand Up @@ -200,26 +192,26 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"// Scala Code: Calling Driver to instantiate Passthrough + PeekPokeTester and execute the test.\n",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment can go away now because of test magic

"// Don't worry about understanding this code; it is very complicated Scala.\n",
"// Think of it more as boilerplate to run a Chisel test.\n",
"val testResult = Driver(() => new Passthrough()) {\n",
" c => new PeekPokeTester(c) {\n",
" poke(c.io.in, 0) // Set our input to value 0\n",
" expect(c.io.out, 0) // Assert that the output correctly has 0\n",
" poke(c.io.in, 1) // Set our input to value 1\n",
" expect(c.io.out, 1) // Assert that the output correctly has 1\n",
" poke(c.io.in, 2) // Set our input to value 2\n",
" expect(c.io.out, 2) // Assert that the output correctly has 2\n",
" }\n",
"test(new Passthrough()) { c =>\n",
" c.io.in.poke(0.U) // Set our input to value 0\n",
" c.io.out.expect(0.U) // Assert that the output correctly has 0\n",
" c.io.in.poke(1.U) // Set our input to value 1\n",
" c.io.out.expect(1.U) // Assert that the output correctly has 1\n",
" c.io.in.poke(2.U) // Set our input to value 2\n",
" c.io.out.expect(2.U) // Assert that the output correctly has 2\n",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have an example anywhere of an expect that fails? Because the current system doesn't return a test success / failure value, it might be useful to demonstrate what happens (exception), and maybe also have a side note on integration w/ ScalaTest

"}\n",
"assert(testResult) // Scala Code: if testResult == false, will throw an error\n",
"println(\"SUCCESS!!\") // Scala Code: if we get here, our tests passed!"
"println(\"SUCCESS!!\") // Scala Code: if we get here, our tests passed!\n",
"\n",
"(new stage.DiagrammerStage).execute(\n",
" Array(\"--target-dir\", \"diagrams\"),\n",
" Seq(chisel3.stage.ChiselGeneratorAnnotation(() => new Passthrough()))\n",
")"
]
},
{
Expand All @@ -228,7 +220,11 @@
"source": [
"What's going on? The test accepts a `Passthrough` module, assigns values to the module's inputs, and checks its outputs. To set an input, we call `poke`. To check an output, we call `expect`. If we don't want to compare the output to an expected value (no assertion), we can `peek` the output instead.\n",
"\n",
"If all `expect` statements are true, then our boilerplate code will return true (see `testResult`)."
"If all `expect` statements are true, then our boilerplate code will return pass.\n",
"\n",
">Note that the `poke` and `expect` use chisel hardware literal notation. Both operations expect literals of the correct type.\n",
"If `poke`ing a `UInt()` you must supply a `UInt` literal (example: `c.io.in.poke(10.U)`, likewise if the input is a `Bool()` the `poke` would expect either `true.B` or `false.B`.\n",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might also be useful to provide a negative example for each, eg for UInt, "this is distinct from a Scala number, eg 10, which will not work and type-error"

"\n"
]
},
{
Expand All @@ -242,16 +238,27 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"val test10result = ???\n",
"// Test with width 10\n",
"\n",
"// Test with width 20\n",
"\n",
"val test20result = ???\n",
"test(new PassthroughGenerator(10)) { c =>\n",
" c.io.in.poke(0.U)\n",
" c.io.out.expect(0.U)\n",
" c.io.in.poke(1023.U)\n",
" c.io.out.expect(1023.U)\n",
"}\n",
"\n",
"test(new PassthroughGenerator(20)) { c =>\n",
" c.io.in.poke(0.U)\n",
" c.io.out.expect(0.U)\n",
" c.io.in.poke(1048575.U)\n",
" c.io.out.expect(1048575.U)\n",
"}\n",
"\n",
"assert((test10result == true) && (test20result == true))\n",
"println(\"SUCCESS!!\") // Scala Code: if we get here, our tests passed!"
]
},
Expand All @@ -264,22 +271,18 @@
"<label for=\"check-1\"><strong>Solution</strong> (click to toggle displaying it)</label>\n",
"<article>\n",
"<pre style=\"background-color:#f7f7f7\">\n",
"val test10result = Driver(() => new PassthroughGenerator(10)) {\n",
" c => new PeekPokeTester(c) {\n",
" poke(c.io.in, 0)\n",
" expect(c.io.out, 0)\n",
" poke(c.io.in, 1023)\n",
" expect(c.io.out, 1023)\n",
" }\n",
"test(new PassthroughGenerator(10)) { c =>\n",
" c.io.in.poke(0.U)\n",
" c.io.out.expect(0.U)\n",
" c.io.in.poke(1023.U)\n",
" c.io.out.expect(1023.U)\n",
"}\n",
"\n",
"val test20result = Driver(() => new PassthroughGenerator(20)) {\n",
" c => new PeekPokeTester(c) {\n",
" poke(c.io.in, 0)\n",
" expect(c.io.out, 0)\n",
" poke(c.io.in, 1048575)\n",
" expect(c.io.out, 1048575)\n",
" }\n",
"test(new PassthroughGenerator(20)) { c =>\n",
" c.io.in.poke(0.U)\n",
" c.io.out.expect(0.U)\n",
" c.io.in.poke(1048575.U)\n",
" c.io.out.expect(1048575.U)\n",
"}\n",
"\n",
"</pre></article></div></section></div>"
Expand All @@ -300,9 +303,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"// Viewing the Verilog for debugging\n",
Expand All @@ -312,9 +313,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"// Viewing the firrtl for debugging\n",
Expand Down Expand Up @@ -352,9 +351,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"class PrintingModule extends Module {\n",
Expand All @@ -371,14 +368,20 @@
" println(s\"Print during generation: Input is ${io.in}\")\n",
"}\n",
"\n",
"class PrintingModuleTester(c: PrintingModule) extends PeekPokeTester(c) {\n",
" poke(c.io.in, 3)\n",
" step(5) // circuit will print\n",
"test(new PrintingModule ) { c =>\n",
" c.io.in.poke(3.U)\n",
" c.clock.step(5) // circuit will print\n",
" \n",
" println(s\"Print during testing: Input is ${peek(c.io.in)}\")\n",
"}\n",
"chisel3.iotesters.Driver( () => new PrintingModule ) { c => new PrintingModuleTester(c) }"
" println(s\"Print during testing: Input is ${c.io.in.peek()}\")\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -394,7 +397,7 @@
"mimetype": "text/x-scala",
"name": "scala",
"nbconvert_exporter": "script",
"version": "2.12.8"
"version": "2.12.10"
}
},
"nbformat": 4,
Expand Down
Loading