Академический Документы
Профессиональный Документы
Культура Документы
Administrative Matters
Readings
Cummings paramdesign paper for hdlcon (posted on class website)
2)
3)
en
cnt[7:0]
clk
Pill Counter
module pill_cnt(clk,rst_n,en,cnt);
input clk,rst_n;
output [7:0] cnt;
input clk,rst_n;
output [7:0] cnt;
module pill_cnt(clk,rst_n,en,cnt);
input clk,rst_n;
output [7:0] cnt;
reg [7:0] cnt;
always @(posedge clk or negedge rst_n)
if (!rst_n)
cnt <= 8h00;
else if (en)
cnt <= cnt + 1; // combinational
endmodule
Is this OK?
Ring Counter
module ring_counter (count, enable, clock, reset);
output reg
[7: 0] count;
input
enable, reset, clock;
always @ (posedge clock or posedge reset)
if (reset == 1'b1)
count <= 8'b0000_0001;
else if (enable == 1b1) begin
case (count)
8b0000_0001: count <= 8b0000_0010;
8b0000_0010: count <= 8b0000_0100;
Rotator
module rotator
output reg
input [7: 0]
input
Shifter
always @ (posedge clk) begin
if (rst) Data_Out <= 0;
else case (select[1:0])
2b00: Data_Out <= Data_Out;
2b01: Data_Out <= {Data_Out[3], Data_Out[3:1]};
2b10: Data_Out <= {Data_Out[2:0], 1b0};
2b11: Data_Out <= Data_In;
endcase
end
endmodule
//
//
//
//
Hold
by 2
X by 2
Parallel Load
module alu(src1,src2,dst,cin,cout);
input [15:0] src1,src2;
//////////////////////////////////
// Instantiate 16-bit adder //
////////////////////////////////
adder #(16) add1(.a(src1),.b(src2),
.cin(cin),.cout(cout),
.sum(dst));
endmodule
12
Examples:
parameter Clk2q = 1.5,
Tsu = 1,
Thd = 0;
Read Cummings
paramdesign paper
for hdlcon posted on
class website
13
State Machines
State Machines:
State Diagrams
b=0 / Y=1
a=0 / Y=1
a=1 / Y=1
a=0
S0
a = 1/
Z=1
S1
b = 1/ Z = 1, Y=1
rst = 1
S2
15
always @ (state,a,b)
case (state)
S0 : if (a) begin
nxt_state = S1;
module fsm(clk,rst,a,b,Y,Z);
Z = 1; end
input clk,rst,a,b;
else
output Y,Z;
nxt_state = S0;
What problems do
S1 : begin
we have here?
parameter S0 = 2b00,
Y=1;
S1 = 2b01,
if (b) begin
S2 = 2b10;
nxt_state = S2;
reg [1:0] state,nxt_state;
Z=1; end
always @(posedge clk, posedge rst)
else
if (rst)
nxt_state = S1;
state <= S0;
end
else
S2 : nxt_state = S0;
state <= nxt_state;
endcase
endmodule
SM Coding
16
S1 : begin
Y=1;
if (b) begin
nxt_state = S2;
Z=1; end
else nxt_state = S1;
end
default : nxt_state = S0;
endcase
endmodule
SM Coding Guidlines
1)
2)
3)
4)
Keep state assignment in separate always block using nonblocking <= assignment
Code state transition logic and output logic together in a
always block using blocking assignments
Assign default values to all outputs, and the nxt_state
registers. This helps avoid unintended latches
Remember to have a default to the case statement.
Default should be (if possible) a state that transitions to the same state
18
SM Interacting with SM
A very common case is a state that needs to be held for a certain time.
The state machine in this case may interact with a timer (counter).
BUS
Cycle
IDLE
ChrgPmp
Enable
tm_eq_3ms/
wrt_done
tm_eq_3ms/inc_tm
clr_tm
+1
comb
tm_eq_3ms
inc_tm
clk
19
20
default : begin
// is CHRG
inc_tm = 1;
chrg_pmp_en=1;
if (tm_eq_3ms)
begin
wrt_done = 1;
nxtState = IDLE;
end
else nxtState = CHRG;
end
endcase
end
assign eep_r_w_n = ~bus_wrt;
assign eep_cs_n = ~bus_wrt;
assign eep_bus = (bus_wrt) ?
wrt_data : 12bzzz;
endmodule
21
MSB
4MHz
Cycles
69
57600baud
69 = 7b1000101
22
USART Example
module usart_tx(clk,rst_n,strt_tx,tx_data,tx_done,TX);
input clk, rst_n, strt_tx;
input [7:0] tx_data;
output TX;
output tx_done;
.
.
.
endmodule;
23
24
26
a
b
g
c
d
=
=
=
=
=
=
=
=
=
=
=
ZERO;
ONE;
TWO;
THREE;
FOUR;
FIVE;
SIX;
SEVEN;
EIGHT;
NINE;
BLANK;
Using the
defined
constants!
29
module intra();
integer a,b;
initial begin
initial begin
a=3;
#6 b = a + a;
#4 a = b + a;
a=3;
b = #6 a + a;
a = #4 b + a;
end
endmodule
end
endmodule
Time
Event
a=3
b=6
10
a=9
Time
Event
a=3
b=6
10
a=9
31
endmodule
module inter2();
integer a,b;
Time
Event
a=3
a=1
b=2
b=3
10
a=4
initial begin
a=3;
b = #6 a + a;
a = #4 b + a;
end
initial begin
#3 a=1;
#5 b=3;
end
Time
Eval
Event
Assign
Event
nxt_b= 6
a=3
--
a=1
nxt_a=7
b=6
--
b=3
10
--
a=7
endmodule
32
Time
Event
b=6
c=9
c=12
33
Time
Event
b=6
d=4
c=5
Intra-Assignment Review
module bnb;
reg a, b, c, d, e, f;
Note: In testbenches I
initial begin // blocking assignments
mainly find blocking intera = #10 1; // a will be assigned 1 at time 10 assignment delays to be the
b = #2 0; // b will be assigned 0 at time 12 most useful. Delays really
c = #4 1; // c will be assigned 1 at time 16 not used outside of
end
testbenches that much
during the design process.
35