6.375 tutorial 2 guards and schedulingcsg.csail.mit.edu/.../t02-schedulingandguards.pdf ·...

26
1 6.375 Tutorial 2 Guards and Scheduling Ming Liu Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-1 Overview BSV reviews/notes Guard lifting EHRs Scheduling Lab 3 Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-2

Upload: others

Post on 17-Feb-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 6.375 Tutorial 2 Guards and Schedulingcsg.csail.mit.edu/.../T02-SchedulingAndGuards.pdf · Simulation executables can dump VCD waveforms ./simMyTest –V test.vcd Produces test.vcd

1

6.375 Tutorial 2

Guards and Scheduling

Ming Liu

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-1

Overview

BSV reviews/notes

Guard lifting

EHRs

Scheduling

Lab 3

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-2

Page 2: 6.375 Tutorial 2 Guards and Schedulingcsg.csail.mit.edu/.../T02-SchedulingAndGuards.pdf · Simulation executables can dump VCD waveforms ./simMyTest –V test.vcd Produces test.vcd

2

Expressions vs. Actions

Expressions Have no side effects (state changes)

Can be used outside of rules and modules in assignments

Actions Can have side effects

Can only take effect when used inside of rules

Can be found in other places intended to be called from rules

Action/ActionValue methods

functions that return actions

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-3

Variable vs. States

Variables are used to name intermediate values

Do not hold values over time

Variable are bound to values Statically elaborated

Feb 19, 2016 http://csg.csail.mit.edu/6.375

Bit#(32) firstElem = aQ.first();

rule process;

aReg <= firstElem;

endrule

T02-4

Page 3: 6.375 Tutorial 2 Guards and Schedulingcsg.csail.mit.edu/.../T02-SchedulingAndGuards.pdf · Simulation executables can dump VCD waveforms ./simMyTest –V test.vcd Produces test.vcd

3

Scoping

Any use of an identifier refers to its declaration in the nearest textually surrounding scope

Feb 19, 2016 http://csg.csail.mit.edu/6.375

Bit#(32) a = 1;

rule process;

aReg <= a;

endrule

module mkShift( Shift#(a) );

function Bit#(32) f();

return fromInteger(valueOf(a))<<2;

endfunction

rule process;

aReg <= f();

endrule

endmodule

Functions can take variables from surrounding scope

T02-5

Guard Lifting

Last Time: implicit/explicit guards

But there is more to it when there are conditionals (if/else) within a rule

Compiler option -aggressive-conditions

tells the compiler to peek into the rule to generate more aggressive enable signals

Almost always used

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-6

Page 4: 6.375 Tutorial 2 Guards and Schedulingcsg.csail.mit.edu/.../T02-SchedulingAndGuards.pdf · Simulation executables can dump VCD waveforms ./simMyTest –V test.vcd Produces test.vcd

4

Guard Examples

Feb 19, 2016 http://csg.csail.mit.edu/6.375

rule process;

if (aReg==True)

aQ.deq();

else

bQ.deq();

$display(“fire”);

endrule

rule process;

if (aQ.notEmpty)

aQ.deq();

$display(“fire”);

endrule

(aReg==True && aQ.notEmpty) ||

(aReg==False && bQ.notEmpty) ||

(aQ.notEmpty && aQ.notEmpty) ||

(!aQ.notEmpty) Always fires

rule process;

aQ.deq();

$display(“fire”);

endrule

T02-7

Ephemeral History Register (EHR)

D Q 0

1 w[0].data

w[0].en

r[0]

normal

bypass r[1]

0

1 w[1].data

w[1].en

r[0] < w[0] w[0] < w[1] < …. r[1] < w[1]

Feb 19, 2016 http://csg.csail.mit.edu/6.375

Encode a notion of “priority” when there are concurrent writes/reads

T02-8

Page 5: 6.375 Tutorial 2 Guards and Schedulingcsg.csail.mit.edu/.../T02-SchedulingAndGuards.pdf · Simulation executables can dump VCD waveforms ./simMyTest –V test.vcd Produces test.vcd

5

Design Example

An Up/Down Counter

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-9

Up/Down Counter Design example

Some modules have inherently conflicting methods that need to be concurrent This example will show a couple of ways to

handle it

interface Counter;

Bit#(8) read;

Action increment;

Action decrement;

endinterface

Feb 19, 2016 http://csg.csail.mit.edu/6.375

Inherently conflicting

T02-10

Page 6: 6.375 Tutorial 2 Guards and Schedulingcsg.csail.mit.edu/.../T02-SchedulingAndGuards.pdf · Simulation executables can dump VCD waveforms ./simMyTest –V test.vcd Produces test.vcd

6

Up/Down Counter Conflicting design

module mkCounter( Counter );

Reg#(Bit#(8)) count <- mkReg(0);

method Bit#(8) read;

return count;

endmethod

method Action increment;

count <= count + 1;

endmethod

method Action decrement;

count <= count – 1;

endmethod

endmodule

Feb 19, 2016 http://csg.csail.mit.edu/6.375

Can’t fire in the same cycle

T02-11

Up/Down Counter Conflicting design

Feb 19, 2016 http://csg.csail.mit.edu/6.375

D Q

+1

-1

read

incr.en || decr.en

T02-12

Page 7: 6.375 Tutorial 2 Guards and Schedulingcsg.csail.mit.edu/.../T02-SchedulingAndGuards.pdf · Simulation executables can dump VCD waveforms ./simMyTest –V test.vcd Produces test.vcd

7

Concurrent Design A general technique

Feb 19, 2016 http://csg.csail.mit.edu/6.375

Replace conflicting registers with EHRs

Choose an order for the methods

Assign ports of the EHR sequentially to the methods depending on the desired schedule

T02-13

Up/Down Counter Concurrent design: read < inc < dec

module mkCounter( Counter );

Ehr#(2, Bit#(8)) count <- mkEhr(0);

method Bit#(8) read;

return count[0];

endmethod

method Action increment;

count[0] <= count[0] + 1;

endmethod

method Action decrement;

count[1] <= count[1] – 1;

endmethod

endmodule

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-14

Page 8: 6.375 Tutorial 2 Guards and Schedulingcsg.csail.mit.edu/.../T02-SchedulingAndGuards.pdf · Simulation executables can dump VCD waveforms ./simMyTest –V test.vcd Produces test.vcd

8

D Q 0

1

incr.en

read

0

1

decr.en

Feb 19, 2016 http://csg.csail.mit.edu/6.375

Up/Down Counter Concurrent design: read < inc < dec

+1

-1

T02-15

D Q 0

1

incr.en

read

0

1

decr.en

Feb 19, 2016 http://csg.csail.mit.edu/6.375

Up/Down Counter Concurrent design: read < inc < dec

+1

-1

T02-16

Page 9: 6.375 Tutorial 2 Guards and Schedulingcsg.csail.mit.edu/.../T02-SchedulingAndGuards.pdf · Simulation executables can dump VCD waveforms ./simMyTest –V test.vcd Produces test.vcd

9

Valid Concurrent Rules

A set of rules ri can fire concurrently if there exists a total order between the rules such that all the method calls within each of the rules can happen in that given order

Rules r1, r2, r3 can fire concurrently if there is an order ri, rj, rk such that ri < rj, rj < rk, and ri < rk are all valid

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-17

Concurrent rule firing

Concurrently executable rules are scheduled to fire in the same cycle

In HW, states change only at clock edges

Rules

HW

Ri Rj Rk

clocks

rule

steps

Ri

Rj

Rk

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-18

Page 10: 6.375 Tutorial 2 Guards and Schedulingcsg.csail.mit.edu/.../T02-SchedulingAndGuards.pdf · Simulation executables can dump VCD waveforms ./simMyTest –V test.vcd Produces test.vcd

10

Rule Scheduling Intuitions

Feb 19, 2016 http://csg.csail.mit.edu/6.375

Can these rules fire in the same cycle?

rule r1 (a);

x <= 1;

endrule

rule r2 (!a);

x <= 2;

endrule

No, guards are mutually exclusive

T02-19

Rule Scheduling Intuitions

Feb 19, 2016 http://csg.csail.mit.edu/6.375

Can these rules fire in the same cycle?

rule r1;

y <= 1;

endrule

rule r2;

x <= 1;

endrule

Yes, methods are unrelated

(conflict free)

T02-20

Page 11: 6.375 Tutorial 2 Guards and Schedulingcsg.csail.mit.edu/.../T02-SchedulingAndGuards.pdf · Simulation executables can dump VCD waveforms ./simMyTest –V test.vcd Produces test.vcd

11

Rule Scheduling Intuitions

Is it legal? Can these rules fire in the same cycle?

rule increment;

x <= x + 1;

endrule

rule decrement;

x <= x - 1;

endrule

Feb 19, 2016 http://csg.csail.mit.edu/6.375

Calls x._read() and x._write()

Calls x._read() and x._write()

increment C decrement, so the two rules will never fire in parallel

T02-21

Rule Scheduling Intuitions

Can these rules fire in the same cycle?

rule r1;

x <= y;

endrule

rule r2;

y <= x;

endrule

Feb 19, 2016 http://csg.csail.mit.edu/6.375

Calls y._read() and x._write()

Calls x._read() and y._write()

r1 C r2, so the two rules will never fire in parallel

T02-22

Page 12: 6.375 Tutorial 2 Guards and Schedulingcsg.csail.mit.edu/.../T02-SchedulingAndGuards.pdf · Simulation executables can dump VCD waveforms ./simMyTest –V test.vcd Produces test.vcd

12

Lab 3: Overview

Completing the audio pipeline:

PitchAdjust

FromMP, ToMP

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-23

Converting C to Hardware

Think about what states you need to keep

Loops in C are sequentially executed; loops in BSV are statically elaborated

Unrolled

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-24

Page 13: 6.375 Tutorial 2 Guards and Schedulingcsg.csail.mit.edu/.../T02-SchedulingAndGuards.pdf · Simulation executables can dump VCD waveforms ./simMyTest –V test.vcd Produces test.vcd

13

Fixed Point

Feb 19, 2016 http://csg.csail.mit.edu/6.375

10.012 = 1x21 + 0x2

0 + 0x2

-1 + 1x2

-2

Twos (21) column

Ones (20) column

Halves (2-1

) column

Fourths (2-2

) column

typedef struct {

Bit#(isize) i;

Bit#(fsize) f;

} FixedPoint#(numeric type isize, numeric type fsize )

T02-25

Fixed Point Arithmetic

Feb 19, 2016 http://csg.csail.mit.edu/6.375

Useful FixedPoint functions:

fxptGetInt: extracts integer portion

fxptMult: full precision multiply

*: full multiply followed by rounding/saturation to the output size

Other useful bit-wise functions:

truncate, truncateLSB

zeroExtend, extend

T02-26

Page 14: 6.375 Tutorial 2 Guards and Schedulingcsg.csail.mit.edu/.../T02-SchedulingAndGuards.pdf · Simulation executables can dump VCD waveforms ./simMyTest –V test.vcd Produces test.vcd

14

BSV Debugging Display Statements

See a bug, not sure what causes it

Add display statements

Recompile

Run

Still see bug, but you have narrowed it down to a smaller portion of code

Repeat with more display statements…

Find bug, fix bug, and remove display statements

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-27

BSV Display Statements

The $display() command is an action that prints statements to the simulation console

Examples: $display(“Hello World!”);

$display(“The value of x is %d”, x);

$display(“The value of y is “,

fshow(y));

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-28

Page 15: 6.375 Tutorial 2 Guards and Schedulingcsg.csail.mit.edu/.../T02-SchedulingAndGuards.pdf · Simulation executables can dump VCD waveforms ./simMyTest –V test.vcd Produces test.vcd

15

Ways to Display Values Format Specifiers

%d – decimal

%b – binary

%o – octal

%h – hexadecimal

%0d, %0b, %0o, %0h

Show value without extra whitespace padding

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-29

Ways to Display Values fshow

fshow is a function in the FShow typeclass

It can be derived for enumerations and structures FixedPoint is also a FShow typeclass

Example:

typedef emun {Red, Blue} Colors deriving(FShow);

Color c = Red;

$display(“c is “, fshow(c));

Feb 19, 2016 http://csg.csail.mit.edu/6.375

Prints “c is Red”

T02-30

Page 16: 6.375 Tutorial 2 Guards and Schedulingcsg.csail.mit.edu/.../T02-SchedulingAndGuards.pdf · Simulation executables can dump VCD waveforms ./simMyTest –V test.vcd Produces test.vcd

16

Warning about $display

$display is an Action within a rule

Guarded methods called by $display will be part of implicit guard of rule

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-31

rule process;

if (aQ.notEmpty)

aQ.deq();

$display(“first elem is %x”, aQ.first);

endrule

Extra Stuff

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-32

Page 17: 6.375 Tutorial 2 Guards and Schedulingcsg.csail.mit.edu/.../T02-SchedulingAndGuards.pdf · Simulation executables can dump VCD waveforms ./simMyTest –V test.vcd Produces test.vcd

17

BSV Debugging Waveform Viewer

Simulation executables can dump VCD waveforms ./simMyTest –V test.vcd

Produces test.vcd containing the values of all the signals used in the simulator Not the same as normal BSV signals

VCD files can be viewed by a waveform viewer Such as gtkwave

The signal names and values in test.vcd can be hard to understand Especially for structures and enumerations

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-33

Step 1

Generate VCD File Run ./simTestName -V test.vcd

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-34

Page 18: 6.375 Tutorial 2 Guards and Schedulingcsg.csail.mit.edu/.../T02-SchedulingAndGuards.pdf · Simulation executables can dump VCD waveforms ./simMyTest –V test.vcd Produces test.vcd

18

Step 2

Open Bluespec GUI Run “bluespec fifo.bspec”

Feb 19, 2016 http://csg.csail.mit.edu/6.375

Note, to run the GUI remotely, you need to SSH into the servers with the “ssh –X” command

For the fifo lab, fifo.bspec can be found in

T02-35

Step 3

Set top module name Open project options

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-36

Page 19: 6.375 Tutorial 2 Guards and Schedulingcsg.csail.mit.edu/.../T02-SchedulingAndGuards.pdf · Simulation executables can dump VCD waveforms ./simMyTest –V test.vcd Produces test.vcd

19

Step 3

Set top module name Set the top module name to match the compiled module in TestBench.bsv

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-37

Step 4

Open Module Viewer

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-38

Page 20: 6.375 Tutorial 2 Guards and Schedulingcsg.csail.mit.edu/.../T02-SchedulingAndGuards.pdf · Simulation executables can dump VCD waveforms ./simMyTest –V test.vcd Produces test.vcd

20

Step 4

Open Module Viewer

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-39

Step 5

Open Wave Viewer

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-40

Page 21: 6.375 Tutorial 2 Guards and Schedulingcsg.csail.mit.edu/.../T02-SchedulingAndGuards.pdf · Simulation executables can dump VCD waveforms ./simMyTest –V test.vcd Produces test.vcd

21

Step 5

Open Wave Viewer

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-41

Feb 19, 2016 http://csg.csail.mit.edu/6.375

Step 6

Open Wave Viewer

T02-42

Page 22: 6.375 Tutorial 2 Guards and Schedulingcsg.csail.mit.edu/.../T02-SchedulingAndGuards.pdf · Simulation executables can dump VCD waveforms ./simMyTest –V test.vcd Produces test.vcd

22

Step 6

Add Some Signals

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-43

Step 6

Add Some Signals

Feb 19, 2016 http://csg.csail.mit.edu/6.375

Module Hierarchy

Signals

T02-44

Page 23: 6.375 Tutorial 2 Guards and Schedulingcsg.csail.mit.edu/.../T02-SchedulingAndGuards.pdf · Simulation executables can dump VCD waveforms ./simMyTest –V test.vcd Produces test.vcd

23

Step 7

Look at the Waveforms

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-45

Step 7

Look at the Waveforms

Feb 19, 2016 http://csg.csail.mit.edu/6.375

Types Human readable value names

T02-46

Page 24: 6.375 Tutorial 2 Guards and Schedulingcsg.csail.mit.edu/.../T02-SchedulingAndGuards.pdf · Simulation executables can dump VCD waveforms ./simMyTest –V test.vcd Produces test.vcd

24

Step 7

Look at the Waveforms

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-47

Step 8

Add Some More Signals

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-48

Page 25: 6.375 Tutorial 2 Guards and Schedulingcsg.csail.mit.edu/.../T02-SchedulingAndGuards.pdf · Simulation executables can dump VCD waveforms ./simMyTest –V test.vcd Produces test.vcd

25

Step 8

Add Some More Signals

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-49

Step 9

Add Rules Too

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-50

Page 26: 6.375 Tutorial 2 Guards and Schedulingcsg.csail.mit.edu/.../T02-SchedulingAndGuards.pdf · Simulation executables can dump VCD waveforms ./simMyTest –V test.vcd Produces test.vcd

26

Step 9

Add Rules Too

Feb 19, 2016 http://csg.csail.mit.edu/6.375 T02-51