Assignment 4
Assignment 4
Implement a synchronous arbitrary counter with states 3 -> 0 -> 1 -> 5 -> 2 -> 7 -> 6 -> 4 -> 9 -> 12. The active low
set signal is used to initialize the counter at any state (valid or invalid) with the input specified using init vector and at
each negative edge of clock signal clk it goes to the next state. At each transition from current state to next state the
counter produces the state count and an output (out) 0, except for the transition from state 12 -> 3 it produces an
output (out) 1 along with the count. The active low reset signal is used to initialize the counter to state 3.
module arb_counter (out, count, init, set, reset, clk);
input set, reset clk;
input [?:0] init;
output [?:0] count;
output out;
From any invalid state the counter will always move to state 5 on the next clock edge. Specify the size of the init and
count vector according to the requirement.
Sample Test Cases
Input Output
Test 0
Pass: for init(count,out)=( 3, 0), after period = 3 arb_counter(count,
Case out)=( 5, 0)
1
Test 1
Pass: for init(count,out)=( 6, 0), after period = 4 arb_counter(count,
Case out)=( 3, 1)
2
Test 2
Pass: for init(count,out)=(15, 0), after period = 1 arb_counter(count,
Case out)=( 5, 0)
3
Test 3
Pass: for init(count,out)=( 3, 0), after period =10 arb_counter(count,
Case out)=( 3, 1)
4
Test 4
Pass: for init(count,out)=( 6, 0), after period =10 arb_counter(count,
Case out)=( 6, 0)
5
Test 5
Pass: for init(count,out)=( 7, 0), after period = 5 arb_counter(count,
Case out)=( 3, 1)
6
Test 6
Pass: for init(count,out)=( 9, 0), after period = 1 arb_counter(count,
Case out)=(12, 0)
7
Test 7
Pass: for init(count,out)=(12, 0), after period = 1 arb_counter(count,
Case out)=( 3, 1)
8
4'b0000: begin
count = 4'b0001;
out = 0;
end
4'b0001: begin
count = 4'b0101;
out = 0;
end
4'b0101: begin
count = 4'b0010;
out = 0;
end
4'b0010: begin
count = 4'b0111;
out = 0;
end
4'b0111: begin
count = 4'b0110;
out = 0;
end
4'b0110: begin
count = 4'b0100;
out = 0;
end
4'b0100: begin
count = 4'b1001;
out = 0;
end
4'b1001: begin
count = 4'b1100;
out = 0;
end
4'b1100: begin
count = 4'b0011;
out = 1;
end
default: begin
count = 4'b0101;
out = 0;
end
endcase
end
end
endmodule
//************************************************************************
//End of Sample Solution
//************************************************************************
`timescale 1ns/1ps
module arb_counter_tb;
reg set, reset, clk;
reg [3:0] init;
wire [3:0] count;
wire out;
reg [3:0] count_p, count_c;
reg out_p, out_c;
initial
begin
clk=1'b1;
forever #10 clk=~clk;
end
//3 -> 0 -> 1 -> 5 -> 2 -> 7 -> 6 -> 4 -> 9 -> 12
initial
begin
ret = $fscanf(STDIN,"%d",testid);
case(testid)
default: begin
$display("Bad testcase id %d",testid);
$finish();
end
endcase
//*******************************************************************************
2. Implement a serial adder that adds two numbers serially bit-by-bit, and produces the sum serially bit-by-bit. The
state transition diagram will consist of two states indicating the carry status, and can be realized using a single state
variable. The reset signal is used to initialize the carry to 0. Suppose we want to add two numbers 01011 and
10011. The inputs are applied sequentially (least significant bit first) to the a and b inputs in synchronism with the
raising edge of clock as (1,1), (1,1), (0,0), (1,0) and (0,1). The sum bits are generated on output s sequentially (least
significant bit first) as 0, 1, 1, 1, 1.
module serialadd (a, b, s, reset, clk);
input a, b, reset, clk;
output s;
Sample Test Cases
Input Output
0 Pass: for a (01011) + b (10011) = s (11110)
Test Case 1
1 Pass: for a (10111) + b (01111) = s (00110)
Test Case 2
2 Pass: for a (10110) + b (00110) = s (11100)
Test Case 3
3 Pass: for a (01000) + b (10001) = s (11001)
Test Case 4
4 Pass: for a (00001) + b (11111) = s (00000)
Test Case 5
5 Pass: for a (11111) + b (00000) = s (11111)
Test Case 6
6 Pass: for a (11111) + b (11111) = s (11110)
Test Case 7
7 Pass: for a (00000) + b (00000) = s (00000)
Test Case 8
//************************************************************************
//Sample Solution
//************************************************************************
//serial adder
module serialadd (a, b, s, reset, clk);
input a, b, reset, clk;
output s;
reg s, cfsm, cfsm_nxt;
endmodule
//************************************************************************
//End of Sample Solution
//************************************************************************
`timescale 1ns/1ps
module serialadd_tb;
reg a, b, reset, clk;
reg [3:0] s_t;
reg [4:0] a_t, b_t;
wire s;
initial
begin
ret = $fscanf(STDIN,"%d",testid);
case(testid)
endcase