Slide 1Example: ShifterCascaded Combinational ShifterAsynchronous pipeline with FIFOs (regs with interlocks)Required simultaneitySynchronous pipeline (with registers)DiscussionExpressions vs. FunctionsFunction ApplicationTypes and type-checkingWhat is a Type?SV notation for typesNumeric type parametersCommon scalar typesSome Composite TypesTypes of variables“let” and type-inferenceType synonyms with typedefEnumerationType safetyStructsBit interpretation of structsTagged UnionsThe Maybe typeSlide 25DerivingDeriving BitsPattern-matchingExample: CPU Instructions OperandsOther types in BSVInstantiating interfaces and modulesModule SyntaxRulesRule predicatesNext LectureFebruary 27, 2006 http://csg.csail.mit.edu/6.375/ L08-1Bluespec-2: TypesArvind Computer Science & Artificial Intelligence LabMassachusetts Institute of TechnologyFebruary 27, 2006 L08-2http://csg.csail.mit.edu/6.375/Example: ShifterGoal: implement: y = shift (x,s)where y is x shifted by s positions.Suppose s is a 3-bit value.Strategy:Shift by s =shift by 4 (=22) if s[2] is set,and by 2 (=21 ) if s[1] is set,and by 1 (=20 ) if s[0] is setA shift by 2j is trivial: it’s just a “lane change” made purely with wires00sh2February 27, 2006 L08-3http://csg.csail.mit.edu/6.375/Cascaded Combinational Shiftersh2sxmuxmuxmuxsh1sh4s0s1s2nnx0x1x23ffunction int shifter (int s,int x); Pair sx0, sx1, sx2; sx0 = step_0(Pair{s:s, x:x}); sx1 = step_1(sx0); sx2 = step_2(sx1); return (sx2.x);endfunctionfunction Pair step_j (Pair sx); return ((sx.s[j]==0) ? sx : Pair{s: sx.s,x:sh_k(sx.x)});endfunctionwhere k=2jA family of functionstypedef struct {int x; int s;} Pair;February 27, 2006 L08-4http://csg.csail.mit.edu/6.375/Asynchronous pipelinewith FIFOs (regs with interlocks)sh2sxmuxmuxmuxsh1sh4s0s1s2nn3rule stage_0 (True); Pair sx0 = fifo0.first(); fifo0.deq(); fifo1.enq(step_0(sx0));endrulefifo0fifo1fifo2fifo3rule stage_1 (True); Pair sx1 = fifo1.first(); fifo1.deq(); fifo2.enq(step_1(sx1));endrulerule stage_2 (True); Pair sx2 = fifo2.first(); fifo2.deq(); fifo3.enq(step_2(sx2));endruleFebruary 27, 2006 L08-5http://csg.csail.mit.edu/6.375/Required simultaneityIf it is necessary for several actions to happen together,(i.e., indivisibly, atomically)Put them in the same rule!February 27, 2006 L08-6http://csg.csail.mit.edu/6.375/Synchronous pipeline(with registers)sh2sxmuxmuxmuxsh1sh4[0] [1] [2]nn3sx1 sx2 sx3sx0rule sync-shifter (True); sx1 <= step_0(sx0); sx2 <= step_1(sx1); sx3 <= step_2(sx2);endrule step_0 step_1 step_2sx1, sx2 and sx3 are registers defined outside of the rulesReg#(Pair) sxi <- mkRegU();Will it start properly?Will it leave some values in the pipe?February 27, 2006 L08-7http://csg.csail.mit.edu/6.375/DiscussionIn the synchronous pipeline, we compose actions in parallelAll stages move data simultaneously, in lockstep (atomic!)In the asynchronous pipeline, we compose rules in parallelStages can move independently (each stage can move when its input fifo has data and its output fifo has room)If we had used parallel action composition instead, all stages would have to move in lockstep, and could only move when all stages were able to moveYour design goals will suggest which kind of composition is appropriate in each situationFebruary 27, 2006 L08-8http://csg.csail.mit.edu/6.375/Expressions vs. FunctionsA function is just an abstraction of a combinational expressionArguments are inputs to the circuitThe result is the output of the circuitxx-const 4xba cdiscrfunction int discr (int a, int b, int c); return b*b – 4*a*c;endfunctionexpressionFebruary 27, 2006 L08-9http://csg.csail.mit.edu/6.375/Function ApplicationInstantiates combinational hardware of the function body Connects the body to argument expressionsxx-const 4xba cdiscrfunction int discr (int a, int b, int c); return b*b – 4*a*c;endfunctiond = discr (10, p, q);const 10p qNo runtime allocation of stack frames or passing of arguments; only meaningful for static elaborationFebruary 27, 2006 L08-10http://csg.csail.mit.edu/6.375/Types and type-checkingBSV is strongly-typedEvery variable and expression has a typeThe Bluespec compiler performs strong type checking to guarantee that values are used only in places that make sense, according to their typeThis catches a huge class of design errors and typos at compile time, i.e., before simulationFebruary 27, 2006 L08-11http://csg.csail.mit.edu/6.375/What is a Type?A type describes a set of valuesTypes are orthogonal (independent) of entities that may carry values (such as wires, registers, …)No inherent connection with storage, or updatingThis is true even of complex types E.g., struct { int …, Bool …}This just represents a set of pairs of values, where the first member of each pair is an int value, and the second member of each pair is a Bool valueFebruary 27, 2006 L08-12http://csg.csail.mit.edu/6.375/SV notation for typesSome types just have a nameMore complex types can have parameters which are themselves typesType names begin with uppercase letterExceptions: ‘int’ and ‘bit’, for compatibility with Verilogint, Bool, Action, …FIFO#(Bool) // fifo containing BooleansTuple2#(int,Bool) // pair of int and BooleanFIFO#(Tuple2#(int,Bool)) // fifo of pairs of int // and Booleanbit[15:0] is the same as Bit#(16)February 27, 2006 L08-13http://csg.csail.mit.edu/6.375/Numeric type parametersBSV types also allows numeric parametersThese numeric types should not be confused with numeric values, even though they use the same number syntaxThe distinction is always clear from context, i.e., type expressions and ordinary expressions are always distinct parts of the program textBit#(16) // 16-bit wide bit-vectorInt#(29) // 29-bit wide signed integersVector#(16,Int#(29)) // vector of 16 whose elements // are of type Int#(29)February 27, 2006 L08-14http://csg.csail.mit.edu/6.375/Common scalar typesBoolBooleansBit#(n)Bit vectors, with a width n bitsInt#(n)Signed integers of n bitsUInt#(n)Unsigned integers of n bitsIntegerUnbound integers; has meaning only during static elaborationFebruary 27, 2006 L08-15http://csg.csail.mit.edu/6.375/Some Composite TypesEnumerationsSets of symbolic namesStructsRecords with fieldsTagged Unionsunions, made “type-safe” with tagsFebruary 27, 2006 L08-16http://csg.csail.mit.edu/6.375/Types of variablesEvery variable has a data type:BSV will enforce proper usage of values
View Full Document