Design Codesign of Embedded Systems Data Types in
Design & Co-design of Embedded Systems Data Types in System. C Maziar Goudarzi 2005 Design & Co-design of Embedded Systems
Today Program z. Bit-accurate data types provided by System. C z. User-defined data types 2005 Design & Co-design of Embedded Systems 2
Data Types in System. C Data Types 2005 Design & Co-design of Embedded Systems
Data Types z. System. C data types y. Single-bit Types xsc_bit, sc_logic y. Integer Types xsc_int, sc_uint, sc_biguint y. Bit-Vector Types xsc_bv, sc_lv y. Fixed-Point Types xsc_fixed, sc_ufixed, sc_fix, sc_ufix 2005 Design & Co-design of Embedded Systems 4
Data Types (cont’d) z. Defined for all types y. Streaming operator defined for all types xostream& operator << ( ostream&, T ); x. Example sc_bit b; cout<<b; y. Trace function xsc_trace(<trace-file pointer>, <traced variable>, <string>) x. Example sc_bit b; sc_trace( tf, b, “message”); 2005 Design & Co-design of Embedded Systems 5
Single-bit Types z sc_bit y. Two-valued logic: ‘ 0’ , ‘ 1’ (character literal) y. Example: sc_bit b; b=‘ 1’; // the same as b=1 or b=true b=‘ 0’; // the same as b=0 or b=false y. Defined operators x. Bitwise: &(and) |(or) x. Assignment: = &= x. Equality: == != 2005 ^(xor) ~(not) |= ^= Design & Co-design of Embedded Systems 6
Single-bit Types (cont’d) z sc_logic y. Four-valued logic: ‘ 0’, ‘ 1’, ‘X’, ‘x’, ‘Z’, ‘z’ (char. literal) x‘x’ means unknown or indeterminate value x‘z’ means high impedance or floating value y. Example: sc_logic l; l=‘ 0’; // the same as l=0, false, sc_logic_0 l=‘ 1’; // the same as l=1, true, sc_logic_1 l=‘z’; // the same as l=2, sc_logic_Z l=‘x; // the same as l=3, sc_logic_X 2005 Design & Co-design of Embedded Systems 7
Single-bit Types (cont’d) z sc_logic (cont’d) y. Defined operators x. Bitwise: &(and) |(or) x. Assignment: = &= x. Equality: == != 2005 ^(xor) ~(not) |= ^= Design & Co-design of Embedded Systems 8
Single-bit Types (cont’d) z Any mixture of sc_bit, sc_logic, and bool types in comparison and assignments is possible, except: sc_logic k; sc_bit b; k = ‘z’; b = k; 2005 // // or bool b or k = ‘x’ Result is undefined. Run-time warning is issued Design & Co-design of Embedded Systems 9
Example: 3 -state buffer SC_MODULE(tristate_buf){ sc_in< sc_bit > input; sc_out< sc_logic > output; sc_in< sc_bit > enable; void process() { sc_bit in, en; sc_logic out; in=input; en=enable; // reading inputs to temporary variables if(en) out = in; else out = 'z'; output = out; // writing a temporary variable to output } SC_CTOR(tristate_buf) { SC_METHOD(process); sensitive<<enable<<input; } Design & Co-design of Embedded }; 2005 Systems 10
Integer Types: Fixed Precision z Fixed Precision Unsigned and Signed Integers y. Integer variables with fixed width (number of bits) ysc_int<n>, sc_uint<n> x 1<= n <=64 y. Signed representation uses 2’s complement y. Underlying implementation: 64 bit integer x-D_32 BIT_ compiler directive y. Can freely be mixed with C++ integers (int, short, long) 2005 Design & Co-design of Embedded Systems 11
Fixed Precision Integers (cont’d) z Defined operators y Bitwise ~ y Arithemtic y Assignment y Equality y Relational y Auto-inc/dec y Bit Select y Part Select y Concatenation 2005 & | + += -= &= |= == != < <= ++ -[]. range(, ) ^ * *= ^= >> / /= = > >= Design & Co-design of Embedded Systems << % %= 12
Fixed Precision Integers (cont’d) z Examples: sc_logic mybit; sc_uint<8> myuint; mybit = myuint[7]; sc_uint<4> myrange; myrange = myuint. range(5, 2); sc_uint<12> my 12 int; my 12 int = (myuint, myrange); 2005 Design & Co-design of Embedded Systems 13
Fixed Precision Integers (cont’d) sc_uint<8> u 1, u 2; sc_int<16> i 1, i 2; u 1 = i 1; // convert int to uint i 2 = u 2; // convert uint to int // BUG! in System. C 2. 0 User’s Guide 2005 Design & Co-design of Embedded Systems 14
Integer Types: Arbitrary Precision Integers z. Integers with (virtually) no width limit y. MAX_NBITSdefined in sc_constants. h ysc_bigint<n>, sc_biguint<n> y. Signed representation uses 2’s complement y. Operators are the same as sc_int, sc_uint y. Can be intermixed with sc_int, sc_uint, and C++ integer types 2005 Design & Co-design of Embedded Systems 15
Bit-Vector Types: Arbitrary Length Bit Vector z Arbitrary Length Bit Vector y. Vector of 2 -valued bits ysc_bv<n> y. No arithmetic operation y. Assignment: String of ‘ 0’ and ‘ 1’ chars y. Reduction operations and_reduce(), nand, or, nor, xnor 2005 sc_bv<6> databus; databus = “ 100100”; sc_logic result; Design & Co-design of Embedded result = databus. or_reduce(); Systems 16
Arbitrary Length Bit Vector (cont’d) z Defined operators y Bitwise ~ y Assignment y Equality y Bit Select y Part Select y Concatenation y Reduction 2005 & | = &= == != []. range(, ) and_reduce() xor_reduce() ^ |= >> ^= << or_reduce() Design & Co-design of Embedded Systems 17
Arbitrary Length Bit Vector (cont’d) z Examples: sc_bv<16> data 16; sc_bv<32> data 32; data 32. range(15, 0) = data 16; data 16 = (data 32. range(7, 0), data 32. range(23, 16)); (data 16. range(3, 0), data 16. range(15, 12)) = data 32. range(7, 0); sc_bit y; sc_bv<8> x; y = x[6]; 2005 sc_bv<16> x; sc_bv<8> y; y = x. range(0, 7); Design & Co-design of Embedded Systems 18
Arbitrary Length Bit Vector (cont’d) z. Arithmetic operations not directly supported sc_bv<8> b 8=“ 11001010”; sc_uint<8> u 8; u 8=b 8; u 8+=5; // or any other arithmetic b 8=u 8; 2005 Design & Co-design of Embedded Systems 19
Bit-Vector Types: Arbitrary Length Logic Vector z. Arbitrary Length Logic Vector y. Vector of 4 -valued bits ysc_lv<n> y. No arithmetic operation x. Any ‘x’ or ‘z’ in the RHS: runtime warning + undefined result y. Assignment: String of ‘ 0’, ‘ 1’, ‘x’, ‘z’ chars y. Operators are the same as sc_bv 2005 Design & Co-design of Embedded Systems 20
Arbitrary Length Logic Vector (cont’d) z. Example: sc_lv<8> bus 1; if(enable) bus 1 = “ 01 xz 10 xx”; else bus 1 = “zzzz”; cout<<bus 1. to_string(); // OR: cout<<bus 1 2005 Design & Co-design of Embedded Systems 21
Example: A Memory Module #define DATA_WIDTH 8 #define ADDR_WIDTH 4 SC_MODULE(memory) { sc_in< sc_bit > chip. Enable; sc_in< sc_bit > read. Write; sc_inout< sc_lv<DATA_WIDTH> > data. Bus; sc_in< sc_lv<ADDR_WIDTH> > addr. Bus; sc_uint<DATA_WIDTH> mem. Array[2^ADDR_WIDTH]; void process() { if ( chip. Enable. read()=='0') { data. Bus. write( "zzzz" ); return; } sc_lv<ADDR_WIDTH> addr_lv = addr. Bus. read(); sc_uint<ADDR_WIDTH> addr = addr_lv; if (read. Write. read()=='1') data. Bus. write( mem. Array[addr] ); else mem. Array[addr]=data. Bus. read(); } SC_CTOR(memory) { SC_METHOD(process); sensitive<<chip. Enable<<read. Write<<data. Bus; Design & Co-design of Embedded // Also: initialize mem. Array to zero 2005} Systems }; 22
Fixed-Point Types z. Usage y. HW implementation of Floating-point arithmetic Floating point representation S Mantissa Exponent Fixed point representation )2’s complement) binary value 2005 Design & Co-design of Embedded Systems 23
Fixed-Point Types (cont’d) z Advantages y Less area y More speed z Disadvantages y Narrower representable range of numbers y Uniform resolution over entire range z Problems to be addressed y Overflow y Quantization z To Be Discussed in Next Session 2005 Design & Co-design of Embedded Systems 24
Data Types in System. C Complementary Issues Regarding Data Types 2005 Design & Co-design of Embedded Systems
Complementary Issues z. Simulation Speed Issues z. User Defined Type Issues y. Equality-check Operator y. Tracing a User-Defined Type 2005 Design & Co-design of Embedded Systems 26
Speed Issues: Integer Types sc_bigint sc_biguint with MAX_NBITS not defined 2005 sc_int sc_uint with -D_32 BIT_ C++ Types with MAX_NBITS defined Design & Co-design of Embedded Systems 27
Speed Issues: Bit and Logic Types 2005 sc_logic sc_bit sc_lv sc_bv Design & Co-design of Embedded Systems 28
User-Defined Type Issues write() new_val==old_val z Required for every type used as signal or port 1. Equality-Check Operator struct packet_type { int info, seq, retry; bool operator == (const packet_type& rhs) const { return (rhs. info == info && rhs. seq == seq && rhs. retry == retry); } Design & Co-design of Embedded }; 2005 Systems 29
User-Defined Type Issues (cont’d) 2. A special trace function void sc_trace(sc_trace_file *tf, const packet_type& v, const sc_string& NAME) { sc_trace(tf, v. info, NAME + ". info"); sc_trace(tf, v. seq, NAME + ". seq"); sc_trace(tf, v. retry, NAME + ". retry"); } 2005 Design & Co-design of Embedded Systems 30
User-Defined Type Issues (cont’d) 3. A special streaming operator ostream & operator <<(ostream &os, packet_type p) { os << p. info << p. seq << p. retry << endl; return os; } 2005 Design & Co-design of Embedded Systems 31
What we learned today z Hardware-centric data types in System. C z User-Defined Data Types y. Required operator y. Tracing Issues 2005 Design & Co-design of Embedded Systems 32
- Slides: 32