Balsa A Language Tutorial Dr Doug Edwards dougcs
Balsa: A Language Tutorial Dr. Doug Edwards doug@cs. man. ac. uk Southampton: Oct 99 Language Tutorial- 1
Structural Iteration constant n = 8 declare procedure buffer_n (input i : word; output o : word) is constant local array 1. . n-1 of channel c : word begin buffer (i, c[1]) || -- first bufferinternal arrayed buffer (c[n-1], o) || -- last buffer channels for || i in 1. . n-2 then -- i’th buffer (c[i], c[i+1]) end parallel compose n-2 internal buffers structural iteration Southampton: Oct 99 Language Tutorial- 2
Micropipeline Buffer procedure buffer (input i : byte; output o : byte) is select “chooses” local variable x : byte begin a single input loop resulting in input “i” select i then being passive x : = i -- store the data end; o <- x select encloses end assignment select statement has enclosed semantics Southampton: Oct 99 Language Tutorial- 3
Examples: Simple Counters Circuits count handshakes on their I/Ps and produce an O/P count bundle n They illustrate: n • strong data typing • data types – arrays, records, enumeration • control structures – if, case • shared procedures Southampton: Oct 99 Language Tutorial- 4
Modulo-16 binary counter procedure count 16 (sync aclk; output count : nibble) is local variable count_reg : nibble begin loop handshake 4 -bit sync aclk ; only input output count <- count_reg ; count_reg : = ( count_reg + 1 as nibble) await h/s end then output count end on input from internal register inc assign cast result back internal to correct variable sizeregister Southampton: Oct 99 Language Tutorial- 5
Passive Input Counter procedure count 16 (sync aclk; output count : nibble) is local variable count_reg : nibble begin using select makes loop count <- count_reg ; aclk a passive i/p select aclk then continue end ; count_reg : = ( count_reg + 1 as nibble) end Southampton: Oct 99 Language Tutorial- 6
Modulo-10 counter procedure count 10(sync aclk; output count: C_size) is local variable count_reg : C_size variable tmp : C_size and begin max_count previously loop declared sync aclk ; if count_reg /= max_count then tmp : = (count_reg + 1 as C_size) temp variable updated in parallel else tmp : = 0 with O/P assignment end || count <- count_reg ; if then else count_reg : = tmp construct end -- loop end Southampton: Oct 99 Language Tutorial- 7
Nested if Statements n Balsa does not have an elseif keyword if condition 1 then proc. A else if condition 2 then proc. B end else proc. C end n Note the explicit sequencing Southampton: Oct 99 Language Tutorial- 8
Parallel Guard Evaluation n If the guards are mutually exclusive, conditions may be tested in parallel: if condition 1 then proc. A | condition 2 then proc. B else proc. C end Southampton: Oct 99 Language Tutorial- 9
Loadable Decade Counter -1 n Illustrates record data type: -- count 10 b. balsa: an aysnchronous loadable decade counter import [balsa. types. basic] public type C_size is nibble constant max_count = 9 type In_bundle is record ld_data : C_size ; ld_ctrl : bit end Southampton: Oct 99 in-line comment record data-structure definition Language Tutorial- 10
Loadable Decade Counter -2 procedure updown 10 (input in_sigs: In_bundle; output count: C_size) is local variable count_reg : C_size variable tmp : C_size begin loop -- main procedure body end body in next slide Southampton: Oct 99 Language Tutorial- 11
Loadable Decade Counter -3 Record selector select in_sigs then if in_sigs. ld_ctrl = 0 then load counter count_reg : = in_sigs. ld_data else Record selector if count_reg /= max_count then enclosed select allows tmp : = (count_reg + 1 as C_size) multiple reads from I/P else tmp : = 0 bundle withoutcount latches increment end || count <- count_reg ; or wrap round count_reg : = tmp end -- end if end -- complete select H/S Southampton: Oct 99 Language Tutorial- 12
Padding Record Structures n Records can be coerced to a fixed length: type Flags is record carry, overflow, zero, negative, int_en: bit over byte Flags is padded to 8 bits Southampton: Oct 99 Language Tutorial- 13
Up/Down Counter n Add another bit to control direction of counting: type C_size is nibble constant max_count = 9 type In_bundle is record ld_data : C_size ; ld_ctrl : bit; dir : bit end Southampton: Oct 99 direction bit Language Tutorial- 14
Up/Down Counter case in_sigs. dir of 0 then -- counting down if count_reg /= 0 then tmp : = (count_reg - 1 as C_size) else tmp : = max_count end || count <- count_reg | 1 then -- counting up if count_reg /= max_count then tmp : = (count_reg + 1 as C_size) else tmp : = 0 end || count <- count_reg end ; -- end case statement Southampton: Oct 99 case count down statement or count up Language Tutorial- 15
Enumeration n Up/down control signal could be defined as: type dir is enumeration down, up end n Values may be aliased values type opcode is enumeration ADD, ADC, SUB, SBC, AND, OR, LD, ST=LD, BR over 3 bits Southampton: Oct 99 Language Tutorial- 16
Shared Procedures -1 type inc is 2 signed bits procedure updown 10 (input in_sigs: In_bundle; output count: inc takes C_size) is values -1 and +1 local variable count_reg : C_size variable tmp : C_size use shared hardware variable inc : inc for counting up and down -- define shared procedure shared update_ctr is begin tmp : = (count_reg + inc as C_size) end -- procedure body shared procedure cast 1 result inc can be +/to correct size Southampton: Oct 99 Language Tutorial- 17
Shared Procedures -2 loop similar code for count up select in_sigs then begin if in_sigs. load_l = 0 then count_reg : = in_sigs. ld_data counting down: else set inc to -1 case in_sigs. dir of down then -- counting down inc : = ( -1 as inc); if count_reg /= 0 then cast update_ctr() required else call tmp : = max_count shared procedure Southampton: Oct 99 Language Tutorial- 18
Arrays Numerically indexed compositions of the same type n Typical use is in the specification of register banks n type Reg. Data is 16 bits variable Reg. Bank : array 0. . 7 of Reg. Data Southampton: Oct 99 Language Tutorial- 19
Register Bank Definition type Reg. Ctrl is record bundle containing Read 0 : Reg. No ; Control info Read 1 : Reg. No ; Write : Reg. No ; Register Do. Read 0 : bit ; -- if true read port 0 identifiers read/write Do. Read 1 : bit ; -- if true read port 1 control bits Do. Write : bit -- if true write data end Control word procedure Reg. Bank ( input Reg. Ctrl : Reg. Ctrl; write dataread data input Write. Port : Reg. Data ; internal output Read. Port 0, Read. Port 1 : Reg. Data ) is local registers variable Reg. Bank : array 0. . 7 of Reg. Data Southampton: Oct 99 Language Tutorial- 20
Register Bank Control write signalled- loop await data, then select Reg. Ctrl then control word if Reg. Ctrl. Do. Write then update register select Write. Port then Reg. Bank[Reg. Ctrl. Write] : = Write. Port two reads can end occur can’t read andtogether write else if Reg. Ctrl. Do. Read 0 then Read. Port 0 <simultaneously Reg. Bank[Reg. Ctrl. Read 0] end || if Reg. Ctrl. Do. Read 1 then Read. Port 1 <Reg. Bank[Reg. Ctrl. Read 1] end end read register end & output to channel Southampton: Oct 99 Language Tutorial- 21
Array Operations n Arrays can be concatenated variable a, b : byte variable c: array 4 of byte variable d : array 6 of byte c: = {a, b, a, b} c: = {a, b} @ {a, b} d: = c @ {a, b} Southampton: Oct 99 -- tuple construction -- array concatenation Language Tutorial- 22
Array Operations n arrays can be sliced variable x : array 0. . 7 of 4 bits variable y : array 0. . 1 of 4 bits y: = x[1. . 2] Southampton: Oct 99 -- returns a slice of x of type -- array 0. . 1 of 4 bits Language Tutorial- 23
Instruction Decoder type Word is 16 signed bits type Imm 5 is 5 signed bits Word and Imm 5 are signed bits variable ICtrl is 8 signed bits -- bottom 5 bits contain an immediate value variable Imm 16 : word Im 16: = (((ICtrl as array 0. . 7 of bit)[0. . 4] as Imm 5) as Word) 5 -bit field is extracted & sign-extended to 16 bits Southampton: Oct 99 Language Tutorial- 24
Instruction Decoder type Word is 16 signed bits type Imm 5 is 5 signed bits variable ICtrl is 8 signed bits -- bottom 5 bits contain an immediate value variable Imm 16 : word Im 16: = (ICtrl as array 0. . 7 of bit) casts Ictrl into an array of bits for slicing Southampton: Oct 99 Language Tutorial- 25
Instruction Decoder type Word is 16 signed bits type Imm 5 is 5 signed bits variable ICtrl is 8 signed bits -- bottom 5 bits contain an immediate value variable Imm 16 : word Im 16: = (ICtrl as array 0. . 7 of bit)[0. . 4] extract bottom 5 bits Southampton: Oct 99 Language Tutorial- 26
Instruction Decoder type Word is 16 signed bits type Imm 5 is 5 signed bits variable ICtrl is 8 signed bits -- bottom 5 bits contain an immediate value variable Imm 16 : word Im 16: = ((ICtrl as array 0. . 7 of bit)[0. . 4] as Imm 5) cast the extracted bits into a 5 -bit signed number Southampton: Oct 99 Language Tutorial- 27
Instruction Decoder type Word is 16 signed bits type Imm 5 is 5 signed bits variable ICtrl is 8 signed bits -- bottom 5 bits contain an immediate value variable Imm 16 : word Im 16: = (((ICtrl as array 0. . 7 of bit)[0. . 4] as Imm 5) as Word) sign-extend the 5 -bit number to 16 bits Southampton: Oct 99 Language Tutorial- 28
The Select Statement n MUX: • Illustrates unbuffered choice – Inputs assumed mutually exclusive • No internal latches procedure mux (input a, b : byte; output c : byte) is begin loop select a then c <- a -- channel behaves like a variable | b then c <- b -- ditto end select gives end choice Southampton: Oct 99 Language Tutorial- 29
Arbitrated Choice public procedure mux (input a, b : byte; output c : byte) is begin loop arbitrate a then c <- a | b then c <- b end arbitrated end choice Southampton: Oct 99 Language Tutorial- 30
Parameterised Procedures Facilitates libraries n Ex: buffer with parameterised width n procedure Buffer ( parameter X : type ; input i : X; output o : X) is X is of type local variable x : X begin vars defined in terms loop of parameterised type i -> x ; o <- x end Southampton: Oct 99 Language Tutorial- 31
Using Parameterised Modules -- pbuffer 1 a. balsa - calling parameterised a procedure import [balsa. types. basic] import [pbuffer 1] public -- instantiate a byte-wide single place buffer procedure test (input a : byte ; output b : byte) is begin Buffer over type byte of a, b end Buffer defined previously Southampton: Oct 99 invoke a buffer of width byte Language Tutorial- 32
Multiple Parameters -1 n Consider a pipeline type cardinal • with parameterised width ranges over natural numbers • with parameterised depth -- Buffer. N: a n-place parameterised, variable width buffer procedure Buffer. N( parameter n : cardinal ; parameter X : type ; input i : X ; output o : X) is local procedure buffer is Buffer over type X begin -- body of procedure end Southampton: Oct 99 Language Tutorial- 33
Multiple Parameters -2 if n = 1 then -- single place pipeline buffer(i, o) | n >= 2 then test for local array 1. . n-1 of channel c : X special cases begin buffer(i, c[1]) || -- first buffer body of buffer(c[n-1], o) || -- last buffer procedure for || i in 1. . n-2 then buffer(c[i], c[i+1]) end define a 4 -deep end byte-wide pipeline else print error, “zero length pipeline specified” end Procedure Buffer 8 is Buffer. N over 4, type byte test for special cases Southampton: Oct 99 Language Tutorial- 34
Recursive Procedures Adding recursion to Balsa allows elegant specifications of many circuits n Consider circuit to generate n handshakes for each call n Divide circuit into odd and even cases n • even case: call itself twice • odd case : issue preliminary h/s and then call itself twice Southampton: Oct 99 Language Tutorial- 35
Handshake Generator -1 procedure Repeat (parameter n : cardinal; sync o ) is begin if n = 0 then procedure print error, “Repeat n must not be 0” name “repeat” | n = 1 then sync o base | n = 2 then sync o ; sync o cases else -- main body of procedure end recursive definition here Southampton: Oct 99 Language Tutorial- 36
Handshake Generator -2 local shared do. Next is begin Repeat over n/2 of (o) define local end begin shared procedure recursive call if (n as bit) then -- n is odd of “repeat” sync o end ; do. Next () end call shared procedure Procedure Gen 5 is Repeat over 5 twice define a 5 x generator Southampton: Oct 99 Language Tutorial- 37
Handshake Multiplier -- Mult. HS: repeat: handshake on `o’ `n’ times for each input import [balsa. types. basic] import [Gen. HS] public procedure Mult. HS (parameter n : cardinal; sync i ; sync o ) is begin loop select i then continue end ; Repeat over n of (o) end procedure repeat end defined earlier -- Here is a x 5 gnerator procedure Mult. HS 5 is Mult. HS over 5 Southampton: Oct 99 Language Tutorial- 38
An n-way multiplexer n Decompose MUX: Southampton: Oct 99 Language Tutorial- 39
An n-way multiplexer -1 -- Pmux 1. balsa: A recursive parameterised MUX definition import [balsa. types. basic] public procedure PMux ( parameter X : type; parameter n : cardinal; array n of input inp : X; output out : X ) is begin -- procedure body width of input number of inputs each input output is a channel Southampton: Oct 99 Language Tutorial- 40
An n-way multiplexer -2 if n = 0 then print error, ”Parameter n should not be zero” | n = 1 then loop select inp[0] -> inp then out <- inp end when data arrives on end either i/p, pass it to o/p | n = 2 then loop select inp[0] -> inp then base cases out <- inp | inp[1] -> inp then out <- inp end Southampton: Oct 99 Language Tutorial- 41
An n-way multiplexer -3 else 2 internal channels local channel out 0, out 1 : X constant mid = n/2 begin PMux over type X, mid of inp[0. . mid-1], out 0 || PMux over type X, n-mid of inp[mid. . n-1], out 1 || PMux over type X, 2 of {out 0, out 1}, out end end two half-size muxs & one 2: 1 mux Southampton: Oct 99 Language Tutorial- 42
Systolic Counters -1 n Kees van Berkel’s design: • Recursively split counter into a head cell & a n/2 tail cell • Head cells may be be odd or even counts Southampton: Oct 99 Language Tutorial- 43
Systolic Counters -2 n Example: • A modulo-11 counter may be constructed 11 = 1 + 2 * 5 11 = 1 + 2 * (1 + 2 * 2) 11 = 1 + 2 * (2 * 1)) Southampton: Oct 99 Language Tutorial- 44
Systolic Counters -3 n Count even cell: procedure c_even(sync a_left, a_right, b_left, b_right) is Choose between begin loop a or b h/s on right select a_right then sync a_left ; sync a_left | b_right then if a then double sync b_left end else end pass b from right to left Southampton: Oct 99 Language Tutorial- 45
Systolic Counters -4 n Count odd cell procedure c_odd(sync a_left, a_right, b_left, b_right) is begin loop first h/s on left sync a_left; select a_right then sync a_left | b_right then choose bewteen a & b sync b_left and pass on end end Southampton: Oct 99 Language Tutorial- 46
Systolic Counters -5 n Base case: count 1 cell procedure c_1(sync a, b) is begin loop sync a; sync b end Southampton: Oct 99 copy handshake from a to b Language Tutorial- 47
Systolic Counters -6 procedure count. N (parameter N : cardinal ; sync a, b) is local sync a_int, b_int 2 internal syncbegin if N = 0 then print error, “Parameter n should not be zero” only channels | N = 1 then c_1(a, b) else plant either an odd if (N as bit) then -- Odd or even channel c_odd(a, a_int, b, b_int) else c_even(a, a_int, b, b_int) this is how we use end || count. N over N/2 of a_int, b_int definition use the counter end and compose with -- OK instantiate an arbitrary counter remainder of counter procedure Ctest is count. N over 53 Southampton: Oct 99 Language Tutorial- 48
Systolic Counters -7 n The count even cell chooses between a and b with a handshake on its right Southampton: Oct 99 Language Tutorial- 49
Count-Even cell n Count even cell: procedure c_even(sync a_left, a_right, b_left, b_right) is begin handshake right encloses loop rest of handshakes select a_right then sync a_left ; sync a_left | b_right then sync b_left end end Southampton: Oct 99 Language Tutorial- 50
Systolic Counters -7 The count even cell chooses between a and b with a handshake on its right n Since choice is implemented by the enclosed select, the handshake ripples down to the far end n • poor cycle time n Need a Tangram style, buffered select • Better perfomance Southampton: Oct 99 Language Tutorial- 51
Tangram style select procedure c_even(sync a_left, a_right, b_left, b_right) is local variable x : bit begin loop Buffer allows h/s select a_right then to complete x : = 0 | b_right then x : = 1 end ; case x of 0 then sync a_left ; sync a_left | 1 then sync b_left end end Southampton: Oct 99 Language Tutorial- 52
Makefile Generation n balsamd automatically generates a makefile for a design • Generates rules for usual source dependencies • Generates rules for making a lard – model of balsa design – test harness for exercising the lard model Southampton: Oct 99 Language Tutorial- 53
Simulation n 3 possibilities • default lard test-harness • balsa test program – balsa is flexible enough to be able to specify many test sequences • custom lard test program – write your own lard Southampton: Oct 99 Language Tutorial- 54
Example Simulation n Lard time view of a modulo-15 systolic counter Southampton: Oct 99 Language Tutorial- 55
Mod-16 Counter (all even) n Balsa style select Southampton: Oct 99 Language Tutorial- 56
Mod-16 Counter (all even) n Tangram Style select Southampton: Oct 99 Language Tutorial- 57
- Slides: 57