Download presentation
Presentation is loading. Please wait.
Published bySurya Sugiarto Modified over 5 years ago
1
BSV Types Constructive Computer Architecture Tutorial 1 Andy Wright
6.175 TA September12, 2014
2
Sequential Circuits - 2 Constructive Computer Architecture Arvind
Computer Science & Artificial Intelligence Lab. Massachusetts Institute of Technology September 10, 2014
3
Sequential Circuit for Multiply
Reg#(Bit#(32)) a <- mkRegU(); Reg#(Bit#(32)) b <- mkRegU(); Reg#(Bit#(32)) prod <-mkRegU(); Reg#(Bit#(32)) tp <- mkReg(0); Reg#(Bit#(6)) i <- mkReg(32); rule mulStep if (i < 32); Bit#(32) m = (a[i]==0)? 0 : b; Bit#(33) sum = add32(m,tp,0); prod[i] <= sum[0]; tp <= sum[32:1]; i <= i+1; endrule September 10, 2014
4
Sequential Circuit for Multiply
Reg#(Bit#(32)) a <- mkRegU(); Reg#(Bit#(32)) b <- mkRegU(); Reg#(Bit#(32)) prod <-mkRegU(); Reg#(Bit#(32)) tp <- mkReg(0); Reg#(Bit#(6)) i <- mkReg(32); rule mulStep if (i < 32); Bit#(32) m = (a[i]==0)? 0 : b; Bit#(33) sum = add32(m,tp,0); prod[i] <= sum[0]; tp <= sum[32:1]; i <= i+1; endrule Dynamic selection September 10, 2014
5
Dynamic selection requires a mux
a[i] a i when the selection indices are regular then it is better to use a shift operator (no gates!) a >> a[0],a[1],a[2],… September 10, 2014
6
Replacing repeated selections by shifts
Reg#(Bit#(32)) a <- mkRegU(); Reg#(Bit#(32)) b <- mkRegU(); Reg#(Bit#(32)) prod <-mkRegU(); Reg#(Bit#(32)) tp <- mkReg(0); Reg#(Bit#(6)) i <- mkReg(32); rule mulStep if (i < 32); Bit#(32) m = (a[0]==0)? 0 : b; a <= a >> 1; Bit#(33) sum = add32(m,tp,0); prod <= {sum[0], prod[31:1]}; tp <= sum[32:1]; i <= i+1; endrule September 10, 2014
7
Replacing repeated selections by shifts
Reg#(Bit#(32)) a <- mkRegU(); Reg#(Bit#(32)) b <- mkRegU(); Reg#(Bit#(32)) prod <-mkRegU(); Reg#(Bit#(32)) tp <- mkReg(0); Reg#(Bit#(6)) i <- mkReg(32); rule mulStep if (i < 32); Bit#(32) m = (a[0]==0)? 0 : b; a <= a >> 1; Bit#(33) sum = add32(m,tp,0); prod <= {sum[0], prod[31:1]}; tp <= sum[32:1]; i <= i+1; endrule Just like dynamic selection optimization, but with dynamic insertion instead September 10, 2014
8
Circuit for Sequential Multiply
bIn aIn << 31:0 s1 b result (high) 31 add 32:1 == 32 done +1 s1 result (low) [30:0] << s1 s1 i a tp prod s2 s2 s2 s2 s1 = start_en s2 = start_en | !done September 10, 2014
9
Circuit analysis Number of add32 circuits has been reduced from 31 to one, though some registers and muxes have been added The longest combinational path has been reduced from 62 FAs to to one add32 plus a few muxes The sequential circuit will take 31 clock cycles to compute an answer September 10, 2014
10
Observations These programs are not very complex and yet it would have been tedious to express these programs in a state table or as a circuit directly BSV method calls are not available in Verilog/VHDL, and thus such programs sometimes require tedious programming Even the meaning of double-write errors is not standardized across tool implementations in Verilog September 10, 2014
11
A subtle problem while(!isDone(x)) { x = doStep(x); } doStep workQ
? doneQ let x = workQ.first; workQ.deq; if (isDone(x)) begin doneQ.enq(x); end else begin workQ.enq(doStep(x)); end Double write problem for 1-element Fifo stay tuned! September 10, 2014
12
BSV Types Constructive Computer Architecture Tutorial 1 Andy Wright
6.175 TA September12, 2014
13
Bit#(numeric type n) The most important type in BSV
We’ll go into the details later September12, 2014
14
Bit#(numeric type n) Literal values: Common functions:
Decimal: 0, 1, 2, … (each have type Bit#(n) Binary: 5’b01101 (13 with type Bit#(5)), 2’b11 (3 with type Bit#(2)) Hex: 5’hD, 2’h3, 16’h1FF0 Common functions: Bitwise Logic: |, &, ^, ~, etc. Arithmetic: +, -, *, %, etc. Indexing: a[i] Concatenation: {a, b} September12, 2014
15
Bool Literal values: Common functions:
True, False Common functions: Boolean Logic: ||, &&, !, ==, !=, etc. All comparison operators (==, !=, >, <, >=, <=) return Bools September12, 2014
16
Int#(n), UInt#(n) Literal values: Common functions: Decimal:
0, 1, 2, … (Int#(n) and UInt#(n)) -1, -2, … (Int#(n)) Common functions: Arithmetic: +, -, *, %, etc. (Int#(n) performs signed operations, UInt#(n) performs unsigned operations) Comparison: >, <, >=, <=, ==, !=, etc. September12, 2014
17
Constructing new types
Renaming types: typedef Enumeration types: enum Compound types: struct vector maybe tagged union September12, 2014
18
typedef Syntax: Basic: Parameterized:
typedef <type> <new_type_name>; Basic: typedef 8 BitsPerWord; typedef Bit#(BitsPerWord) Word; Can’t be used with parameter: Word#(n) Parameterized: typedef Bit#(TMul#(BitsPerWord,n)) Word#(n); Can’t be used without parameter: Word September12, 2014
19
enum typedef enum {red, blue} Color deriving (Bits, Eq);
Creates the type Color with values red and blue Can create registers containing colors Reg#(Color) Values can be compared with == and != September12, 2014
20
struct typedef struct { Bit#(12) address; Bit#(8) data; Bool write_en;
} MemReq deriving (Bits, Eq); Elements from MemReq x can be accessed with x.address, x.data, x.write_en September12, 2014
21
Tuples Types: Values: Accessing an element: Tuple2#(type t1, type t2)
Tuple3#(type t1, type t2, type t3) up to Tuple8 Values: tuple2( x, y ), tuple3( x, y, z ), … Accessing an element: tpl_1( tuple2(x, y) ) = x tpl_2( tuple3(x, y, z) ) = y … September12, 2014
22
Vector Type: Values: Functions: Can contain registers or modules
Vector#(numeric type size, type data_type) Values: newVector(), replicate(val) Functions: Access an element: [] Rotate functions Advanced functions: zip, map, fold Can contain registers or modules Must have ‘import Vector::*;’ in BSV file September12, 2014
23
Maybe#(t) Type: Values: Functions: Maybe#(type t) tagged Invalid
tagged Valid x (where x is a value of type t) Functions: isValid(x) Returns true if x is valid fromMaybe(default, m) If m is valid, returns the valid value of m if m is valid, otherwise returns default Commonly used fromMaybe(?, m) September12, 2014
24
tagged union Maybe is a special type of tagged union
typedef union tagged { void Invalid; t Valid; } Maybe#(type t) deriving (Eq, Bits); Tagged unions are collections of types and tags. The type contained in the union depends on the tag of the union. If tagged Valid, this type contains a value of type t September12, 2014
25
tagged union – Continued
Values: tagged <tag> value Pattern matching to get values: case (x) matches tagged Valid .a : return a; tagged Invalid : return 0; endcase See BSV Reference Guide (on course website) for more examples of pattern matching September12, 2014
26
Reg#(t) State element of BSV
Main state element in BSV Type: Reg#(type data_type) Instantiated differently from normal variables Uses <- notation Written to differently from normal variables Uses <= notation Can only be done inside of rules and methods Reg#(Bit#(32)) a_reg <- mkReg(0) // value set to 0 Reg#(Bit#(32)) b_reg <- mkRegU() // uninitialized // write to b_reg (needs to be done inside rule) b_reg <= 7; September12, 2014
27
Reg and Vector Register of Vectors Vector of Registers
Reg#( Vector#(32, Bit#(32) ) ) rfile; rfile <- mkReg( replicate(0) ); Vector of Registers Vector#( 32, Reg#(Bit#(32)) ) rfile; rfile <- replicateM( mkReg(0) ); Each has its own advantages and disadvantages September12, 2014
28
Partial Writes Reg#(Bit#(8)) r; Reg#(Vector#(8, Bit#(1))) r
r[0] <= 0 counts as a read and write to the entire register r let r_new = r; r_new[0] = 0; r <= r_new Reg#(Vector#(8, Bit#(1))) r Same problem, r[0] <= 0 counts as a read and write to the entire register r[0] <= 0; r[1] <= 1 counts as two writes to register r – double write problem Vector#(8,Reg#(Bit#(1))) r r is 8 different registers r[0] <= 0 is only a write to register r[0] r[0] <= 0 ; r[1] <= 1 is not a double write problem September12, 2014
29
Modules Modules are building blocks for larger systems
Modules contain other modules and rules Modules are accessed through their interface module mkAdder( Adder#(32) ); Adder#(32) is the interface September12, 2014
30
Interfaces Interfaces contain methods for other modules to interact with the given module Interfaces can also contain other interfaces interface MyInterface#(numeric type n); method ActionValue#(Bit#(b)) f(); interface SubInterface s; endinterface September12, 2014
31
Interface Methods Method Action ActionValue
Returns value, doesn’t change state method Bit#(32) peek_at_front(); Action Changes state, doesn’t return value method Action enqueue(); ActionValue Changes state, returns value method ActionValue#(Bit#(32)) dequeue_front() September12, 2014
32
Strong Typing The Bluespec Compiler throws errors if it can’t figure out a type Which of the following lines work? Bit#(32) a = 7; Bit#(8) small_b = 3; let b = zeroExtend( small_b ); let a_plus_b = a + b; Bit#(8) b_plus_fifty_truncated = truncate( b + 50 ); September12, 2014
33
Quiz September12, 2014
34
Question 1 What is the type of a? Bit#(n) x = 1; Bit#(m) y = 3;
let a = {x,y}; Bit#(TAdd#(n,m)) September12, 2014
35
Question 2 What is the type of b? Bit#(n) x = 1; Bit#(m) y = 3;
let a = {x,y}; let b = x + y; Type Error! + expects inputs and outputs to all have the same type September12, 2014
36
Question 2 – BSC Error Error: “File.bsv”, line 10, column 9: … Type error at: y Expected type: Bit#(n) Inferred type: Bit#(m) September12, 2014
37
Question 3 What is the type of c? Bit#(8) x = 9; let c = x[0]; Bit#(1)
September12, 2014
38
Question 4 What is the type of d? Bit#(8) x = 9;
let d = zeroExtend(x); Can’t tell, so the compiler gives a type error September12, 2014
39
Question 5 What does this function do? How does it work?
function Bit#(m) resize(Bit#(n) x) Bit#(m) y = truncate(zeroExtend(x)); return y; endfunction Produces a compiler error! zeroExtend(x) has an unknown type September12, 2014
40
Question 5 – Fixed function Bit#(m) resize(Bit#(n) x) Bit#(TMax#(m,n)) x_ext; x_ext = zeroExtend(x); Bit#(m) y = truncate(x_ext); return y; endfunction September12, 2014
41
Question 6 What does this code do? // mainQ, redQ, blueQ are FIFOs
// redC, blueC let x = mainQ.first; mainQ.deq; if( isRed(x) ) redQ.enq(x); redC <= redC + 1; if( isBlue(x) ) blueQ.enq(x); blueC <= blueC + 1; Not what it looks like September12, 2014
42
Question 6 – Rewritten let x = mainQ.first; mainQ.deq; if( isRed(x) ) redQ.enq(x); redC <= redC + 1; if( isBlue(x) ) blueQ.enq(x); blueC <= blueC + 1; Only the first action/expression after the if is done, that’s why we have begin/end September12, 2014
43
Question 6 – Fixed let x = mainQ.first; mainQ.deq; if( isRed(x) ) begin redQ.enq(x); redC <= redC + 1; end if( isBlue(x) ) begin blueQ.enq(x); blueC <= blueC + 1; September12, 2014
44
Why is Bit#(n) so important?
Bit#(n) is the only type that can be synthesized as a set of wires Other types are synthesizable only if they can be converted to Bit#(n) How do you keep track of types that can be converted to Bit#(n)? Typeclasses! (specifically the Bits#(t) typeclass) September12, 2014
45
Bits#(a,n) typeclass typeclass Bits#(type a, type n); function Bit#(n) pack(a x); function a unpack(Bit#(n) y); endtypeclass If there is an instance of the typeclass Bits#(mytype, n), then mytype can be converted to Bits#(n) using pack. Furthermore, mytype can be stored in a register of type Reg#(mytype) September12, 2014
46
Bits#(a,n) instance typedef enum { red, green, blue } Color deriving (Eq); // but not bits instance Bits#(Color, 2); function Bit#(2) pack(a x); if( x == red ) return 0; else if( x == green ) return 1; else return 2; function Color unpack(Bit#(2) y) if( x == 0 ) return red; else if( x == 1 ) return green; else return blue; endfunction endinstance September12, 2014
47
Typeclasses Typeclasses allow polymorphism across types Examples:
Eq: contains == and != Ord: contains <, >, <=, >=, etc. Bits: contains pack and unpack Arith: contains arithmetic functions Bitwise: contains bitwise logic September12, 2014
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.