[go: up one dir, main page]

0% found this document useful (0 votes)
13 views41 pages

Lecture5 - SystemC Datatype

The document provides an overview of various data types in SystemC, including sc_bit, sc_logic, sc_bv<W>, sc_lv<W>, sc_int<W>, sc_uint<W>, sc_bigint<W>, sc_biguint<W>, and fixed-point types. It details their characteristics, ranges, precision, and examples of usage, along with the operations and classes associated with each type. Additionally, it discusses quantization and saturation modes for fixed-point types, outlining how they handle precision and overflow in arithmetic operations.

Uploaded by

aa25001492
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views41 pages

Lecture5 - SystemC Datatype

The document provides an overview of various data types in SystemC, including sc_bit, sc_logic, sc_bv<W>, sc_lv<W>, sc_int<W>, sc_uint<W>, sc_bigint<W>, sc_biguint<W>, and fixed-point types. It details their characteristics, ranges, precision, and examples of usage, along with the operations and classes associated with each type. Additionally, it discusses quantization and saturation modes for fixed-point types, outlining how they handle precision and overflow in arithmetic operations.

Uploaded by

aa25001492
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Datatype

Kun-Chih (Jimmy) Chen 陳坤志

Department of Computer Science and Engineering


National Sun Yat-sen University
Overview

❖ SystemC provides the designer the ability to use any and all C++ data
types as well as unique SystemC data types to model systems

Type Description Range Precision


sc_bit 2 value single bit type 0,1
sc_logic 4 value single bit type 0,1,Z,X
sc_bv<W> arbitrary sized 2 value vector type 0,1
sc_lv<W> arbitrary sized 4 value vector type 0,1,Z,X
sc_int<W> 1~64 bit signed integer type -2W-1 ~ 2W-1-1 1
sc_uint<W> 1~64 bit unsigned integer type 0 ~ 2W-1 1
sc_bigint<W> arbitrary sized signed integer type -2W-1 ~ 2W-1-1 1
sc_biguint<W> arbitrary sized unsigned integer type 0 ~ 2W-1 1
sc_fixed<w,iw,q,o,n> templated signed fix point type -2iw-1 ~ 2iw-1-2iw-w 2iw-w
sc_ufixed<w,iw,q,o,n> templated unsigned fix point type 0 ~ 2iw-2iw-w 2iw-w
sc_fix(OPs) untemplated signed fix point type -2iw-1 ~ 2iw-1-2iw-w 2iw-w
sc_ufix(OPs) untemplated unsigned fix point type 0 ~ 2iw-2iw-w 2iw-w

P2
sc_bit

❖ Two valued data type representing a single bit

❖ Can have the value ’0’(false) or ’1’(true) only

❖ Useful for modeling parts of the design where ‘Z’ (high impedance) or
‘X’ (unknown) values are not needed

Classes Operators
Bitwise ~ (not) & (and) | (or) ^ (xor)
Assignment = &= |= ^=
Equality == !=

P3
Example: sc_bit

sc_bit a, b, c; //declaration
bool d;

a = ‘0’; //use char literals


b = ‘1’;

a = a & b;
a &= b; //equivalent

d = true;
c = a | d; //can be mixed with C/C++ bool type

P4
sc_logic

❖ A more general single bit data type

❖ Can have the value ’0’(false) , ’1’(true) , ‘Z’ (high impedance) or ‘X’
(unknown)

❖ Most common data type used for simulations at the RTL

Classes Operators
Bitwise ~ (not) & (and) | (or) ^ (xor)
Assignment = &= |= ^=
Equality == !=

P5
Example: sc_logic

bool w;
sc_bit x;
sc_logic y, z;

w = x == y; //sc_bit and sc_logic


w = y != z; //sc_logic and sc_logic
w = y == ‘1’; //sc_logic and literal

y = x; //sc_bit to sc_logic
x = y; //sc_logic to sc_bit

/*if the value of y is ’Z’ or ’X’ when assignment occurs, the result of
the assignment is undefined and a runtime warning is issued*/

P6
Example: 3-state buffer

input output

enable

P7
sc_bv<W>

❖ Two valued arbitrary length vector to be used for large bit vector
manipulation

Classes Operators
Bitwise ~ & | ^ << >>
Assignment = &= |= ^=
Equality == !=
Bit selection []
Part selection range()
Concatenation (,)
Reduction and_reduce() or_reduce() xor_reduce()

P8
Example: sc_bv<W>

sc_bit w;
sc_bv<8> x,y;
sc_bv<16> z;

z = “1111111111111111”; //string of 0 and 1 can be assigned to sc_bv


y = “00000000”

w = z.or_reduce(); //or all bits in z together

w = z[4]; //single bit selection

x = z.range(7,0) //part selection

P9
Example: sc_bv<W>

sc_bit w;
sc_bv<8> x, y;
sc_bv<16> z;

z = (x, y); //concatenation of bit vectors

y = (z.range(15,12), z.range(3,0)); //mixed concatenation and part selection

(x.range(4,1), y.range(6,3)) = z.range(11,4);


/*bit selection, part selection and concatenation work on both sides of an
assignment operator and in expressions*/

cout<<“z = “<<z.to_string(); //print a human readable character string

P10
sc_lv<W>

❖ Similar to sc_bv but can have ‘Z’ and ‘X’ value

❖ Always slower than sc_bv type in simulation

Classes Operators
Bitwise ~ & | ^ << >>
Assignment = &= |= ^=
Equality == !=
Bit selection []
Part selection range()
Concatenation (,)
Reduction and_reduce() or_reduce() xor_reduce()

P11
Example: sc_lv<W>

P12
sc_int<W> , sc_uint<W>

❖ Fixed Precision Unsigned and Signed Integers


❖ Integer variables with fixed width (number of bits)
➢ Provides up to 64 bits (size of long int in C/C++ language)
❖ Signed representation uses 2’s complement

Classes Operators
Bitwise ~ & | ^ << >>
Arithmetic + - * / %
Assignment = += -= *= /= %= &= |= ^=
Equality == !=
Relational < <= > >=
Autoincrement ++
Autodecrement --
Bit selection []
Part selection range()
Concatenation (,)

P13
Example: sc_int<W> , sc_uint<W>

int w;
sc_bit b;
sc_int<16> x, y;
sc_uint<24> z;

w = 200;

z = w * 2; //sc_int and sc_uint can be used with int


//bits that exceed 24 are removed
b = z[3];
x = z.range(19, 4) ; //selection operations work on sc_int or sc_uint

P14
Example: sc_int<W> , sc_uint<W>

y = x<<4;
z.range(15, 0) = x & y; //bitwise operations

y += x.range(7, 4);
x++;
//perform compound-assignment and autoincrement like int

z = (x.range(7, 0), y);


/*concatenation operation can be used to make a larger value from one or
more smaller values*/

What are the results of x, y, and z ?

P15
sc_bigint<W> , sc_biguint<W>

❖ Integer with (virtually) no width limit


❖ Provides up to MAX_NBITS bits (defaulted to 512)
❖ MAX_NBITS defined in sc_constants.h

❖ Signed representation uses 2’s complement


❖ Operators are the same as sc_int, sc_uint
Classes Operators
Bitwise ~ & | ^ << >>
Arithmetic + - * / %
Assignment = += -= *= /= %= &= |= ^=
Equality == !=
Relational < <= > >=
Autoincrement ++
Autodecrement --
Bit selection []
Part selection range()
Concatenation (,)
P16
Speed Issue

❖ Integer Types

❖ Bit and Logic Types

P17
Fixed Point Types (1/4)

❖ When designers model at a high level, floating point numbers are useful to
model arithmetic operations

❖ can handle a very large range of values


❖ easily scaled

❖ In hardware floating point data types are typically converted or built as fixed-
point data types

❖ to minimize the amount of hardware needed to implement the functionality

P18
Fixed Point Types (2/4)

❖ 4 basic fixed-point type in SystemC

❖ sc_fixed ❖ sc_fix

❖ sc_ufixed ❖ sc_ufix

❖ sc_fixed and sc_ufixed uses static arguments to specify the functionality


❖ setup at compile time and do not change

❖ sc_fix and sc_ufix can use argument types that are nonstatic
❖ sc_fix and sc_ufix can use variables to determine arguments at runtime

P19
Fixed Point Types (3/4)

❖ No mixing of signed / unsigned / other type is allowed

❖ When perform binary bitwise operations, two operands are aligned by


the binary point (.)
❖ the maximum word length and maximum fractional word length are taken
❖ Both operands are converted to this type first

Classes Operators
Bitwise ~ & | ^
Arithmetic + - * / % << >>
Assignment = += -= *= /= %= &= |= ^=
Equality == !=
Relational < <= > >=
Autoincrement ++
Autodecrement --
Bit selection [] (return sc_fxnum_bitref)
Part selection range() (return sc_fxnum_subref)
P20
Fixed Point Types (4/4)

❖ Fixed point type is declared with the following syntax:

❖ sc_fixed<wl, iwl, q_mode, o_mode, n_bits> x;

❖ sc_ufixed<wl, iwl, q_mode, o_mode, n_bits> y;

❖ sc_fix x(list of options);

❖ sc_ufix y(list of options);

P21
arguments for sc_fixed and sc_ufixed (1/2)

❖ wl - Total word length

❖ the total number of bits used in the type

❖ iwl - Integer word length

❖ the number of bits that are to the left of the binary point (.)

P22
arguments for sc_fixed and sc_ufixed (2/2)

❖ q_mode - Quantization mode

❖ determines the behavior of the fixed point type when the result of an operation
generates more precision in the least significant bits than is available as specified
by the word length and integer word length parameters

❖ o_mode - Overflow mode

❖ determines the behavior of the fixed point most significant bits when an operation
generates more precision in the most significant bits than is available

❖ n_bits - Number of saturated bits

❖ only used for overflow mode and specifies how many bits will be saturated if a
saturation behavior is specified and an overflow occurs

P23
wl and iwl

❖ wl
❖ must be greater than 0

❖ iwl
❖ can be positive or negative, and larger than the word length

wl iwl representation signed range unsigned range


5 7 xxxxx00. [-64,60] [0,124]
5 5 xxxxx. [-16,15] [0,31]
5 3 xxx.xx [-4,3.75] [0,7.75]
5 1 x.xxxx [-1,0.9375] [0,1.9375]
5 0 0.xxxxx [-0.5,0.46875] [0,0.96875]
5 -2 0.ssxxxxx [-0.125,0.109375] [0,0.234375]
1 -1 0.sx [-0.25,0] [0,0.25]

P24
Quantization Mode

❖ Determine what happens to the LSBs when more bits of precision are
required than are available

❖ EX : 1011.011 assigned to a sc_fixed in format of xxxx.xx


1011.011

Quantization Mode Name


Round SC_RND
Round towards zero SC_RND_ZERO
Round towards minus infinity SC_RND_MIN_INF
Round towards infinity SC_RND_INF
Convergent rounding SC_RND_CONV
Truncate (default) SC_TRN
Truncation toward zero SC_TRN_ZERO

P25
SC_RAND

❖ Round the value to the closest representable number

❖ Accomplished by adding the MSB of the removed bits to the


remaining bits

❖ 1011.011 → 101101 + 1 → 101110 → 1011.10

P26
SC_RAND_ZERO

❖ Perform SC_RND if the two nearest representable numbers are not


equal distance apart

❖ Otherwise rounding to zero will be performed

❖ For positive numbers the redundant bits are simply deleted, for
negative numbers the MSB of the deleted bits is added to the
remaining bits

❖ 1011.011 → 101101 + 1

→ 101110 → 1011.10

P27
SC_RAND_MIN_INF

❖ Perform SC_RND if the two nearest representable numbers are not


equal distance apart
❖ Otherwise round towards minus infinity by eliminating the redundant
bits

❖ 1011.011 → 1011.01

P28
SC_RAND_INF

❖ Perform SC_RND if the two nearest representable numbers are not equal
distance apart, otherwise the number is rounded towards plus infinity if
positive or minus infinity if negative

❖ For positive numbers the MSB of the deleted bits is added to the remaining
bits, for negative numbers the redundant bits are deleted

❖ 1011.011 → 1011.01

P29
SC_RAND_CONV

❖ Perform SC_RND if the two nearest representable numbers are not equal
distance apart

❖ Otherwise this mode checks the LSB of the remaining bits, if the LSB is 1 this
mode will round towards plus infinity, if the LSB is 0 this mode will round
towards minus infinity

❖ 1011.011 → 101101 + 1

→ 101110 → 1011.10

P30
SC_TRN

❖ The default quantization mode to be used if no other value is specified

❖ The result is always rounded towards minus infinity

❖ The redundant bits are always deleted no matter whether the number is
positive or negative

❖ 1011.011 → 1011.01

P31
SC_TRN_ZERO

❖ Perform SC_RND for positive numbers

❖ For negative numbers the result is rounded towards zero

❖ Accomplished by deleting the redundant bits on the right side and adding the
sign bit to the LSBs of the remaining bits

❖ Only occurs if at least one of the deleted bits is nonzero

❖ 1011.011 → 101101 + 1 → 101110 → 1011.10

P32
Saturation Mode

❖ Determine what happens when the result of an operation generates


more bits on the MSB side than are available

❖ EX : 1011.011 assigned to a sc_fixed in format of xxx.xxx


1011.011

Saturation Mode Name


Saturation SC_SAT
Saturation to zero SC_SAT_ZERO
Symmetrical saturation SC_SAT_SYM
n_bits = 0 (default)
Wrap-around SC_WRAP
n_bits > 0
n_bits = 0
Sign magnitude wrap-around SC_WRAP_SM
n_bits > 0

P33
SC_SAT

❖ Convert the specified value to MAX for overflow or MIN for an underflow

❖ 1011.011 → 1 011.011 → 100.000

P34
SC_SAT_ZERO

❖ Set the result to 0 for any input value that is outside the representable range

❖ 1011.011 → 1 011.011 → 000.000

P35
SC_SAT_SYM

❖ Convert the specified value, defined by designer, to MAX for overflow or -


MAX for an underflow

❖ 1011.011 → 1 011.011 → 101.000

What is the difference between SC_SAT and SC_SAT_SYM?


P36
SC_WRAP , n_bits = 0

❖ The default overflow mode

❖ Any MSB bits outside the range of the target type are deleted

❖ 1011.011 → 1 011.011 → 011.011

P37
SC_WRAP , n_bits > 0

❖ n_bit MSB bits are to be saturated

❖ The sign bit is retained so that positive numbers remain positive and negative
numbers remain negative

❖ The bits that are not saturated are simply copied

For n_bits = 2

1011.011 → 1 011.011 → 101.011

P38
SC_WRAP_SM , n_bits = 0

❖ Delete any MSB bits that are outside the result word length

❖ If the MSB of remaining bit is different from the LSB of deleted bits, all the
remaining bits are inverted

❖ 1011.011 → 1 011.011 → 100.100

P39
SC_WRAP_SM , n_bits > 0

❖ Delete any MSB bits that are outside the result word length

❖ n_bits MSB bits will be saturated and the sign bit retained

❖ If the LSB of saturation bits is different from the original bit, the remaining bits
are inverted

❖ For n_bits = 2

❖ 1011.011 → 1 011.011

→ 101.011 → 100.100

P40
Performance issue

❖ Datatypes that close to C++ built-in types would be faster

Datatypes

Fastest C++ built-in types (e.g. int, char and bool)

sc_int<> sc_uint<>

sc_bit sc_bv<>

sc_logic sc_lv<>

sc_bigint<> sc_biguint<>

Slowest sc_fixed<> sc_fix sc_ufixed<> sc_ufix

P42

You might also like