unit-03-digital_sys
unit-03-digital_sys
Digital systems
Computer Structure
E.T.S.I. Informática
Universidad de Sevilla
CL
s[2:0] ALU v z
3 clk
The data unit includes the processing elements needed by the system: registers, counters,
ALUs, etc. and an interconnection system.
The control unit organize the activation of control signals to the elements of the data unit to
accomplish some task or group of tasks: implement a control algorithm, do some complex
calculation, execute a program (in computers).
start
op din reset
Buses can be dedicated: connect just two devices, but most of the
time we use “bus” to refer to shared connections among various
devices.
Shared buses save resources but reduce performance.
Modern systems tend to use more and more dedicated buses.
Register (or other devices) inputs are all connected to the same bus.
Any register can be the destination of the data in the bus.
A control signal in the register (eg. write enable) makes the desired
register to load the data.
Drawback: different registers cannot be written with different data at
the same time.
Bidirectional
connection
Only one register can be read to the bus at a given time, so all
registers need to be able to disconnect from the bus (tri-state
output).
The same bus will probably be the input bus to other devices.
Registers may have a bidirectional connection to the bus.
This is a form of bus multiplexing: many data sources.
The ALU can get all the needed data and calculate the result in one clock cycle.
The result can be saved to the target register in the same clock cycle.
Only one bus for ALU input data. One operand have to be transferred to a temporary register (T).
An extra clock cycle is needed to transfer the operand to T.
T is a hidden register. No macro-operation uses it, but it is needed by the architecture “internally”.
Only one bus for all ALU input and output. Needs another temporary register to
store the result (A).
Another clock cycle (3) is needed to transfer the result to its destination.
RTL operation
or State box: represents an state of the FSM. The name of the
signal activation state is written next to the box. Includes the RTL operations
or signal activation that occurs while in that state.
Represents the Moore’s behavior of the state.
Decision box: the condition inside the box is tested and the path
0 1 out of the box is selected accordingly.
condition
Micro-operations
module calculator (
input ... RTL design with HDL (Verilog):
);
output ...
describe the system in Verilog. It
always @(posedge clk)
enables:
...
- Testing by simulation.
- Automatic logic synthesis.
// T register
always @(posedge clk)
if (reset == 1'b0)
regt <= 'b0;
else if (wt)
regt <= bus;
op = 3; // SWAP(A,B)
start = 1'b1;
@(posedge clk) #1;
start = 1'b0;
[...]
Departamento de Tecnología Electrónica – Universidad de Sevilla 51
Simple calculator. Top-level system design
and FPGA implementation
●
The implementation in the development
board requires some additional devices:
A – 7-segment display controller: to convert
binary data to drive the 7-segment display.
B
– An inverter to make the reset signal active-
high (easier to control with a push button).
– An edge detecting circuit to convert a
start button press in a single pulse.
op din reset
module system (
input ...
output ...
);
calculator ...
display_ctrl ...
edge_detector ...
// Edge detector
always @(posedge(clk)) begin
start0 <= start;
if (start == 1'b1 && start0 == 1'b0)
start1 <= 1'b1;
else
start1 <= 1'b0;
end
// Calculator instance
calculator #(.W(W)) calculator (
// File: system.v .clk(clk), // global clock
// Project: simple calculator .reset(~reset), // reset (active-low)
// Description: Calculator system. .start(start1), // start
// Author: Jorge Juan-Chico <[email protected]> .din(din), // input data
// Date: 2023-02-12 (initial version) .op(op), // operation code
.ready(ready), // ready for next operation
module system #( .rega(rega), // Register A
parameter W = 4 .regb(regb) // Register B
)( );
input wire clk, // clock (rising edge)
input wire reset, // reset button (active-high)// 7-segment controller instance
input wire start, // start button display_ctrl #(.cdbits(18), .hex(1)) display_ctrl (
input wire [W-1:0] din, // data input .ck(clk), // system clock
input wire [1:0] op, // operation input .x3(4'h0), // display digits, from left to right
output wire ready, // ready indicator .x2(rega),
output wire [3:0] an, // 7-segment anode control .x1(4'h0),
output wire [0:6] seg, // 7-segment code (active-low) .x0(regb),
output wire dp // 7-segment decimal point output.dp_in(4'b1011), // decimal point vector
); .seg(seg), // 7-segment output
.an(an), // anode output
// Internal signals .dp(dp) // decimal point output
wire [W-1:0] rega, regb; );
reg start0, start1; endmodule
synthesis bitstream
tools
restrictions
file
programmer
start
op din reset
(7 + 5) * 2 7 75+2* 6 24
3 * (5 - 2) / (9 - 1) 13 352-*91-/ 12 1.125
3 * (5 - 2) ^ (7 - 3) / ((9 - 1) * 352-73-^*91
27 22 2.53125
(8 + 4)) -84+*/
ov c
x
+/-
x -
+
enter
The control unit reads the active input (enter, add, sub or pm)
and generates the sequence of control signals for the data unit
to perform the required operation.
Control unit + data unit form our calculator, but we still lack the
interface to display x_in and y_reg in the 7-segment display.
Macro-operation
Micro-operations Micro-operations
name for each macro for each macro
(register transfer) (signal activation)
always @* begin
c = 1'b0; v = 1'b0;
case(op)
`ALU_ADD: begin
r = a + b;
// Design: Simple RPN calculator c = a[7] & b[7] | b[7] & ~r[7] | a[7] & ~r[7];
// File: alu.v v = ~a[7] & ~b[7] & r[7] | a[7] & b[7] & ~r[7];
// Description: Arithmetic-(Logic) Unit end
// Author: Jorge Juan-Chico <[email protected]> `ALU_SUB: begin
// Date: 2021-02-27 (initial) r = a - b;
c = ~a[7] & b[7] | b[7] & r[7] | ~a[7] & r[7];
`define ALU_ADD 0 v = a[7] & ~b[7] & ~r[7] | ~a[7] & b[7] & r[7];
`define ALU_SUB 1 end
`define ALU_TRA 2 `ALU_TRA: begin
`define ALU_NEGB 3 r = a;
end
module alu #( `ALU_NEGB: begin
parameter W = 8 // Data width r = -b;
)( c = |b;
input wire [W-1:0] a, // data input a v = b[7] & r[7] | ~b[7] & ~r[7];
input wire [W-1:0] b, // data input b end
input wire [1:0] op, // operation selector default: // Should not happen
output reg c, // carry carry output flag r = 'bx;
output reg v, // overflow flag endcase
output reg [W-1:0] r // output result end
); endmodule
module data_unit #(
parameter W = 8 // Data width
)(
input wire clk,
input wire [W-1:0] x_in,
input wire [1:0] op,
input wire y_w,
input wire s_w,
output reg [W-1:0] yreg, // Y register
output wire f_c, // flag carry
output wire f_v // flag overflow
);
wire [W-1:0] alu_out;
wire c, v;
reg [1:0] sreg; // Status register
// ALU instance
alu #(.W(8)) alu (.a(x_in),.b(yreg),.op(op),
Register Y and the status register are );
.c(c),.v(v),.alu_out(alu_out)
reg clk; reg [7:0] x_in; reg [1:0] op; reg y_w; reg s_w;
wire [7:0] yreg; // Y register
wire f_c; // flag carry
wire f_v; // flag overflow
Y = Y + 125 [...]
module control_unit (
input wire clk, // clock (rising edge)
input wire reset, // reset (synchronous, active-low)
input wire enter, // enter key
input wire add, // add key
input wire sub, // subtract key
input wire pm, // plus/minus key
// State definition
localparam [1:0] READY = 0,
WAIT = 1;
// State variables
reg [1:0] state, next_state;
[...]
The control unit is a standard FSM. The code can be directly derived
from the ASM control chart (see DEC unit about FSMs).
The code adds a reset signal, that must be present in every FSM
implementation.
// Output printing
$display("reset enter add sub pm : op y_w s_w");
$monitor(" %b %b %b %b %b : %d %b %b",
reset, enter, add, sub, pm, op, y_w, s_w);
// Operation
@(posedge clk) #1; // wait one clock cycle and
reset = 1'b0; // reset
@(posedge clk) #1;
The test bench just activates some inputs of reset = 1'b1;
the control unit in sequence: repeat(3) @(posedge clk) #1;
enter = 1'b1; // press "enter"
- enter a number, repeat(3) @(posedge clk) #1;
- change the sign, enter = 1'b0;
repeat(3) @(posedge clk) #1;
- add with the previous number, pm = 1'b1; // press "pm"
- enter another number, repeat(3) @(posedge clk) #1;
- subtract to the previous number. pm = 1'b0;
[...]
$finish; // finish the simulation
end
endmodule
Departamento de Tecnología Electrónica – Universidad de Sevilla 79
WE1 Verilog modeling
Stage 4: calculator module
// Design: Simple RPN calculator
// File: calculator.v
module calculator #(
parameter W = 8 // Data width
)(
input wire clk, // clock (rising edge)
input wire reset, // reset (synchronous, active-low)
input wire enter, // enter key
input wire add, // add key
input wire sub, // subtract key
input wire pm, // plus/minus key
input wire [W-1:0] x_in, // data input
output wire [W-1:0] yreg, // data output (Y register)
output wire f_c, // flag carry
output wire f_v // flag overflow
);
// Internal signals
wire [1:0] op;
wire y_w, s_w;
// Output printing
$display("reset enter add sub pm : x_in yreg f_c f_v");
$monitor(" %b %b %b %b %b : %d %d %b %b",
reset, enter, add, sub, pm, x_in, yreg, f_c, f_v);
// Operation
@(posedge clk) #1; // wait one clock cycle and do
This test bench is very similar to the control reset = 1'b0; // reset
unit, but now the data unit should generate @(posedge clk) #1;
the correct result. It does: reset = 1'b1;
repeat(3) @(posedge clk) #1;
- enter number 7,
- change the sign, x_in = 7; // Y <- 7
enter = 1'b1; // press "enter"
- enter number 20 and add, repeat(3) @(posedge clk) #1;
- enter number 5 and subtract. enter = 1'b0;
repeat(3) @(posedge clk) #1;
[...]
$finish; // finish the simulation
end
endmodule
Departamento de Tecnología Electrónica – Universidad de Sevilla 81
WE1 Verilog modeling
Stage 5: top-level system and test
// Design: Simple RPN calculator
// File: system.v
module system (
input wire clk, // clock (rising edge)
// input wire reset, // reset (synchronous, active-low)
input wire enter, // enter key
input wire add, // add key
input wire sub, // subtract key
input wire pm, // plus/minus key
input wire [7:0] x_in, // data input
output wire f_c, // flag carry
output wire f_v, // flag overflow
output wire [3:0] an, // 7-segment anode control
output wire [0:6] seg, // 7-segment code (active-low)
output wire dp // 7-segment decimal point output
);
// Internal signals
wire [7:0] yreg;
// Calculator instance
calculator #(.W(8)) calculator (
.clk(clk), // clock (rising edge)
.reset(1'b1), // reset (synchronous, active-low)
.enter(enter), // enter key
This is the module that will be implemented in the [...]
);
development board.
It just connects the calculator to the 7-segment // 7-segment controller instance
display_ctrl #(.cdbits(18), .hex(1)) display_ctrl (
display controller we have taken from elsewhere. .ck(clk), // system clock
.x3(x_in[7:4]), // display digits, from left to right
Note that the reset input has been disabled by .x2(x_in[3:0]),
setting it to 1. If necessary, it could be connected .x1(yreg[7:4]),
to a GPIO port. .x0(yreg[3:0]),
.dp_in(4'b1011), // decimal point vector
We won't simulate it and just test in a FPGA chip. .seg(seg), // 7-segment output
.an(an), // anode output
We also need a “restrictions” file for the synthesis .dp(dp) // decimal point output
tool. );
endmodule
Departamento de Tecnología Electrónica – Universidad de Sevilla 82
WE1 Verilog modeling
Source files necessary at each stage
●
Stage 1: ALU design and simulation
– alu.v, alu_tb.v
●
Stage 2: Data unit design and simulation
– + data_unit.v, data_unit_tb.v
●
Stage 3: Control unit design and simulation
– + control_unit.v, control_unit_tb.v
●
Stage 4: Calculator design and simulation
– + calculator.v, calculator_tb.v
●
Stage 5: Top-level system design, implementation and test
– + display_ctrl.v, system.v, system.ucf
WE2 operation
●
“enter” loads new data in the stack from d_in.
●
“calc” perform a calculation given by a code in
d_in.
●
Operations use X or X and Y.
●
The result is always stored in X.
●
Operations are two's complement.
Step 2 Step 3
Assign values to the operation codes in d_in. Specify the operations to be implemented by
the ALU.
Step 4 Step 5
Specify the operations to be implemented by Write the micro-operations for the selected
the registers and their control signals. architecture that are needed to execute each
macro-operation.
Step 6 Step 7
Draw the RTL and control ASM chart of a Describe the system in Verilog. Decide which
control unit for the system. modules to design and simulate independently
Step 8
Map the system's input/output signals to a development board and implement it. (Include
additional peripherals in a top-level design if needed).
Departamento de Tecnología Electrónica – Universidad de Sevilla 87