chisel @ cs250 part i lecture 02cs250/fa13/lectures/lec02.pdf · chisel 6 constructing hardware in...

49
Chisel @ CS250 – Part I – Lecture 02 Jonathan Bachrach EECS UC Berkeley September 3, 2013

Upload: others

Post on 17-Mar-2020

22 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Chisel @ CS250 – Part I – Lecture 02

Jonathan Bachrach

EECS UC Berkeley

September 3, 2013

Page 2: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Standard Design Methodology 1

Specification

Simulation Synthesis

Hierarchically defines structure and function

of circuit.

Verification: Does the design behave as required with regards

to function (and timing, and power consumption)?

Maps design to resources of implementation platform

(FGPA or ASIC).

Page 3: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Design Entry 2

Design circuits graphicallyUsed commonly untilapproximately 2002Schematics are intuitiveLabor intensive to produce(especially readable ones).Requires a special editor toolUnless hierarchy is carefullydesigned, schematics can beconfusing and difficult to followon large designs

Page 4: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Hardware Description Languages 3

Structural Description:connections of components witha nearly one-to-onecorrespondence to schematicdiagram.

Decoder(output x0,x1,x2,x3;

input a,b) {

wire abar, bbar;

inv(bbar, b);

inv(abar, a);

and(x0, abar, bbar);

and(x1, abar, b );

and(x2, a, bbar);

and(x3, a, b );

}

Behavioral Description: usehigh-level constructs (similar toconvential programming) todescribe the circuit function.

Decoder(output x0,x1,x2,x3;

input a,b) {

case [a b]

00: [x0 x1 x2 x3] = 0x1;

01: [x0 x1 x2 x3] = 0x2;

10: [x0 x1 x2 x3] = 0x4;

11: [x0 x1 x2 x3] = 0x8;

endcase;

}

Page 5: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Verilog Issues 4

Originally invented for simulationMany constructs don’t synthesize: ex: deassign, timing constructsOthers lead to mysterious results: for-loopsDifficult to understand synthesis implications of proceduralassignments (always blocks), and blocking versus non-blockingassignmentsIn common use, most users ignore much of the language and stickto a very strict styleVery weak meta programming support for creating circuit generatorsVarious hacks around this over the years, ex: embedded TCLscriptingVHDL has much the same issues

Page 6: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Traditional Hardware Design Process 5

specifydesign

writeverilog

verify

synthesizeand layout

specify designgenerator

write generator(python, perl)

verify

synthesizeand layout

writeverilog

Page 7: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Chisel 6Constructing Hardware In Scala Embedded Language

Embed a hardware-description language in Scala, using Scala’sextension facilitiesChisel is just a set of class definitions in Scala and when you write aChisel program you are actually writing a Scala programA hardware module is just a data structure in ScalaClean simple set of design construction primitives for RTL designFull power of Scala for writing hardware generatorsDifferent output routines can generate different types of output (C,FPGA-Verilog, ASIC-Verilog) from same hardware representationCan be extended above with domain specific languages (such asdeclarative cache coherence specifications)Can be extended below with new backends (such as quantum)Open source with lots of librariesOnly 5200 lines of code in current version!

Page 8: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Chisel Workflow 7

Chisel Program

C++ Code FPGA Verilog ASIC Verilog

Scala / JVM

C++ Compiler FPGA Tools ASIC Tools

C++ Simulator FPGA Emulation GDS Layout

Page 9: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

The Scala Programming Language 8

Compiled to JVMGood performanceGreat Java interoperabilityMature debugging, execution environments

Object OrientedFactory Objects, ClassesTraits, overloading etc

FunctionalHigher order functionsAnonymous functionsCurrying etc

ExtensibleDomain Specific Languages (DSLs)

Page 10: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Chisel Hardware Design Process 9

specify designgenerator

write generator(chisel+scala)

verify

synthesizeand layout

generateverilog

Page 11: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Scala Bindings 10

// constant

val x = 1

val (x, y) = (1, 2)

// variable

var y = 2

y = 3

Page 12: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Scala Collections 11

// Array’s

val tbl = new Array[Int](256)

tbl(0) = 32

val y = tbl(0)

val n = tbl.length

// ArrayBuffer’s

import

val buf = new ArrayBuffer[Int]()

buf += 12

val z = buf(0)

val l = buf.length

// List’s

val els = List(1, 2, 3)

val (x, y, z) = (1, 2, 3)

val els2 = x :: y :: y :: Nil

val a :: b :: c :: Nil = els

val m = els.length

Page 13: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Scala Iteration 12

val tbl = new Array[Int](256)

// loop over all indices

for (i <- 0 until tbl.length)

tbl(i) = i

// nested loop

for (i <- 0 until 16; j <- 0 until 16)

tbl(j*16 + i) = i

// loop of each sequence element

val tbl2 = new ArrayBuffer[Int]

for (e <- tbl)

tbl2 += 2*e

// create second table with doubled elements

val tbl2 = for (i <- 0 until 16) yield tbl(i)*2

Page 14: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Scala Functions 13

// simple scaling function, e.g., x2(3) => 6

def x2 (x: Int) = 2 * x

// more complicated function with statements

def f (x: Int, y: Int) = {

val xy = x + y;

if (x < y) xy else -xy

}

Page 15: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Scala Functional 14

// simple scaling function, e.g., x2(3) => 6

def x2 (x: Int) = 2 * x

// produce list of 2 * elements, e.g., x2list(List(1, 2, 3)) => List(2, 4, 6)

def x2list (xs: List[Int]) = xs.map(x2)

// simple addition function, e.g., add(1, 2) => 3

def add (x: Int, y: Int) = x + y

// sum all elements using pairwise reduction, e.g., sum(List(1, 2, 3)) => 6

def sum (xs: List[Int]) = xs.foldLeft(0)(add)

Page 16: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Scala Object Oriented 15

class Blimp(r: Double) {

val rad = r

println("Another Blimp")

}

new Blimp(10.0)

class Zep(h: Boolean, r: Double) extends Blimp(r) {

val isHydrogen = r

}

new Zep(true, 100.0)

Page 17: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Scala Singleton Objects 16

object Blimp {

var numBlimps = 0

def apply(r: Double) = {

numBlimps += 1

new Blimp(r)

}

}

Blimp.numBlimps

Blimp(10.0)

Page 18: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Cloning 17

shallow copy of objectuser can override method to incorporate parametersthis.type allows precise return types

class Blimp(r: Double) {

val rad = r

override def clone(): this.type = new Blimp(r)

}

val b1 = new Blimp(10)

val b2 = b1.clone()

Page 19: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Scala Console 18

> scala

scala> 1 + 2

=> 3

scala> def f (x: Int) = 2 * x

=> (Int) => Int

scala> f(4)

=> 8

Page 20: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Chisel Example 19

class Mux2 extends Module {

val io = new Bundle{

val sel = UInt(INPUT, 1)

val in0 = UInt(INPUT, 1)

val in1 = UInt(INPUT, 1)

val out = UInt(OUTPUT, 1)

}

io.out := (io.sel & io.in1) |

(~io.sel & io.in0)

}

Mux2~

&

&

|UInt

UInt

UInt

in0

in1

sel

UInt

out

Page 21: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Literals 20

UInt(1) // decimal 1-bit literal from Scala Int.

UInt("ha") // hexadecimal 4-bit literal from string.

UInt("o12") // octal 4-bit literal from string.

UInt("b1010") // binary 4-bit literal from string.

SInt(5) // signed decimal 4-bit literal from Scala Int.

SInt(-8) // negative decimal 4-bit literal from Scala Int.

UInt(5) // unsigned decimal 3-bit literal from Scala Int.

Bool(true) // Bool literals from Scala literals.

Bool(false)

Page 22: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Literals 21

UInt("h_dead_beef") // 32-bit literal of type UInt.

UInt(1) // decimal 1-bit literal from Scala Int.

UInt("ha", 8) // hexadecimal 8-bit literal of type UInt.

UInt("o12", 6) // octal 6-bit literal of type UInt.

UInt("b1010", 12) // binary 12-bit literal of type UInt.

SInt(5, 7) // signed decimal 7-bit literal of type SInt.

UInt(5, 8) // unsigned decimal 8-bit literal of type UInt.

Page 23: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Literal Node Construction 22

UInt(1)

UInt(1)

Page 24: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Algebraic Construction 23

UInt(1) + UInt(2)

UInt(1)

UInt(2)

+

Page 25: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Combinational Circuits 24

(sel & in1) | (~sel & in0)

~

in0 &

in1 &

sel |

Page 26: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Fan Out 25

val sel = a | b

val out = (sel & in1) | (~sel & in0)

~

in0 &

in1 &

|a

b

| outsel

Page 27: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Wires 26

val sel = UInt()

val out = (sel & in1) | (~sel & in0)

sel := a | b

~

in0 &

in1 &

|a

b

| outUIntsel

Page 28: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Bitwise operators 27

Valid on UInt, SInt, Bool.

// Bitwise-NOT

val invertedX = ~x

// Bitwise-AND

val hiBits = x & UInt("h_ffff_0000")

// Bitwise-OR

val flagsOut = flagsIn | overflow

// Bitwise-XOR

val flagsOut = flagsIn ^ toggle

Page 29: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Bitwise reductions 28

Valid on UInt and SInt. Returns Bool.

// AND-reduction

val allSet = andR(x)

// OR-reduction

val anySet = orR(x)

// XOR-reduction

val parity = xorR(x)

where reduction applies the operation to all the bits.

Page 30: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Equality comparison 29

Valid on UInt, SInt, and Bool. Returns Bool.

// Equality

val equ = x === y

// Inequality

val neq = x != y

where === is used instead of == to avoid collision with Scala.

Page 31: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Shifts 30

Valid on SInt and UInt.

// Logical left shift.

val twoToTheX = SInt(1) << x

// Right shift (logical on UInt & UInt, arithmetic on SInt).

val hiBits = x >> UInt(16)

where logical is a raw shift and arithmetic performs top bit sign extension.

Page 32: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Bitfield manipulation 31

Valid on SInt, UInt, and Bool.

// Extract single bit, LSB has index 0.

val xLSB = x(0)

// Extract bit field from end to start bit pos.

val xTopNibble = x(15,12)

// Replicate a bit string multiple times.

val usDebt = Fill(3, UInt("hA"))

// Concatenates bit fields, w/ first arg on left

val float = Cat(sgn,exp,man)

Page 33: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Logical Operations 32

Valid on Bools.

// Logical NOT.

val sleep = !busy

// Logical AND.

val hit = tagMatch && valid

// Logical OR.

val stall = src1busy || src2busy

// Two-input mux where sel is a Bool.

val out = Mux(sel, inTrue, inFalse)

Page 34: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Arithmetic operations 33

Valid on Nums: SInt and UInt.

// Addition.

val sum = a + b

// Subtraction.

val diff = a - b

// Multiplication.

val prod = a * b

// Division.

val div = a / b

// Modulus

val mod = a % b

where SInt is a signed fixed-point number represented in two’scomplement and UInt is an unsigned fixed-point number.

Page 35: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Arithmetic comparisons 34

Valid on Nums: SInt and UInt. Returns Bool.

// Greater than.

val gt = a > b

// Greater than or equal.

val gte = a >= b

// Less than.

val lt = a < b

// Less than or equal.

val lte = a <= b

Page 36: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Bitwidth Inference 35

operation bit widthz = x + y wz = max(wx, wy)

z = x - y wz = max(wx, wy)

z = x & y wz = min(wx, wy)

z = x | y wz = max(wx, wy)

z = Mux(c, x, y) wz = max(wx, wy)

z = w * y wz = wx + wy

z = x << n wz = wx + maxNum(n)

z = x >> n wz = wx - minNum(n)

z = Cat(x, y) wz = wx + wy

z = Fill(n, x) wz = wx * maxNum(n)

Page 37: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Functional Abstraction 36

def mux2 (sel: UInt, in0: UInt, in1: UInt) =

(sel & in1) | (~sel & in0)

val out = mux2(k,a,b)

~

b &

k &

a | out

Page 38: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Bundles 37

class MyFloat extends Bundle {

val sign = Bool()

val exponent = UInt(width = 8)

val significand = UInt(width = 23)

}

val x = new MyFloat()

val xs = x.sign

UInt

UInt

Bool

sig

exp

sig

Page 39: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Vecs 38

// Vector of 3 23-bit signed integers.

val myVec = Vec.fill(3) { SInt(width = 23) }

can be used as Scala sequencescan also be nested into Chisel Bundles

SInt

SInt

SInt

2

1

0

Page 40: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Static Vec Element Access 39

val myVec = Vec.fill(3) { SInt(width = 23) }

// Connect to one vector element chosen at elaboration time.

val sint0 = myVec(0)

val sint1 = myVec(1)

fix1 := data1

myVec(2) := data2

SInt

SInt

SInt

2

1

0

data2

data1 sint1

sint0

Page 41: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Dynamic Vec Element Access 40

val myVec = Vec.fill(3) { SInt(width = 23) }

// Connect to one vector element chosen at runtime.

val out0 = myVec(addr0)

val out1 = myVec(addr1)

myVec(addr2) := data2

SInt

SInt

SInt

2

1

0

addr2

data2 deselector

selector

addr0

out0

selector

addr1

out1

Page 42: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Ports 41

Data object with directions assignedto its members

class Decoupled extends Bundle {

val data = UInt(INPUT, 32)

val valid = Bool(OUTPUT)

val ready = Bool(INPUT)

}

Direction assigned at instantiationtime

class ScaleIO extends Bundle {

val in = new MyFloat().asInput

val scale = new MyFloat().asInput

val out = new MyFloat().asOutput

}

Bool

Bool

UInt

ready

valid

data

Page 43: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Module 42

inherits from Module class,contains an interface storedin a port field named io, andwires together subcircuits inits constructor.

class Mux2 extends Module {

val io = new Bundle{

val sel = UInt(INPUT, 1)

val in0 = UInt(INPUT, 1)

val in1 = UInt(INPUT, 1)

val out = UInt(OUTPUT, 1)

}

io.out := (io.sel & io.in1) |

(~io.sel & io.in0)

}

Mux2~

&

&

|UInt

UInt

UInt

in0

in1

sel

UInt

out

Page 44: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Chisel Workflow 43

Mux2.scala bytecodes

Mux2.cpp

scalacompiler

jvmchiselbuilder

jvm cpp backend

Mux2g++

+

1 2

Mux2.v

jvm verilog backend

verificationvcs

simulation

exec

net list + power, area, and speed estimates

dc

Page 45: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

State Elements 44

Reg(next = in)

Regin

Page 46: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Rising Edge 45

def risingEdge(x: Bool) = x && !Reg(next = x)

Reg

x

!

&&

Page 47: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Counter 46

def counter(max: UInt) = {

val x = Reg(init = UInt(0, max.getWidth))

x := Mux(x === max, UInt(0), x + UInt(1))

x

}

RegMux

UInt(0)

===

maxUInt(1)

+

UInt(0)

reset

tst

consequent

alternate

Page 48: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Sequential Circuits 47

// Produce pulse every n cycles.

def pulse(n: UInt) = counter(n - UInt(1)) === UInt(0)

// Flip internal state when input true.

def toggle(p: Bool) = {

val x = Reg(init = Bool(false))

x := Mux(p, !x, x)

x

}

// Square wave where each half cycle has given period.

def squareWave(period: UInt) = toggle(pulse(period))

Page 49: Chisel @ CS250 Part I Lecture 02cs250/fa13/lectures/lec02.pdf · Chisel 6 Constructing Hardware In Scala Embedded Language Embed a hardware-description language in Scala, using Scala’s

Simple Two Step RTL Semantics 48

// reset

eval_combinational(true);

assign_next_state(true);

// execution

loop {

eval_combinational(false);

assign_next_state(false);

}