Skip to content

Commit b448476

Browse files
committed
feat: example 1-3 and cocotb for example 1
0 parents  commit b448476

File tree

9 files changed

+186
-0
lines changed

9 files changed

+186
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.vcd
2+
*.vvp
3+
*.out
4+
__pycache__
5+
sim_build
6+
results.xml

ex01_and_gate/and_gate.v

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module and_gate(a, b, out);
2+
input a, b;
3+
output out;
4+
5+
assign out = a & b;
6+
7+
endmodule

ex01_and_gate/and_gate_tb.v

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
`timescale 1ns/1ns
2+
3+
module and_gate_tb();
4+
reg a, b;
5+
wire out;
6+
7+
and_gate uut(a, b, out);
8+
9+
initial begin
10+
$dumpfile("and_gate_tb.vcd");
11+
$dumpvars(0, and_gate_tb);
12+
13+
a = 0;
14+
b = 0;
15+
#10 a = 1;
16+
#10 b = 1;
17+
#10 a = 0;
18+
#10 b = 0;
19+
#10 a = 1;
20+
#10 b = 0;
21+
#10 a = 0;
22+
#10 b = 1;
23+
#10 $finish;
24+
end
25+
26+
endmodule

ex01_and_gate/test/Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Simulator
2+
SIM ?= icarus
3+
4+
# Top level language
5+
TOPLEVEL_LANG ?= verilog
6+
7+
# Files
8+
VERILOG_SOURCES = $(shell pwd)/../and_gate.v
9+
10+
# TOPLEVEL is the name of the toplevel module in your Verilog or VHDL file
11+
TOPLEVEL = and_gate
12+
13+
# MODULE is the basename of the Python test file
14+
MODULE = and_gate_test
15+
16+
# include cocotb's make rules to take care of the simulator setup
17+
include $(shell cocotb-config --makefiles)/Makefile.sim
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import cocotb
2+
from cocotb.triggers import Timer
3+
4+
@cocotb.test()
5+
async def and_gate_test(dut):
6+
a = (0, 0, 1, 1)
7+
b = (0, 1, 0, 1)
8+
out = (0, 0, 0, 1)
9+
10+
for i in range(4):
11+
dut.a.value = a[i]
12+
dut.b.value = b[i]
13+
await Timer(1, units='ns')
14+
assert dut.out.value == out[i], f"[Iteration {i}] output is {dut.out.value}, expected {out[i]}"

ex02_1_bit_full_adder/full_adder.v

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module full_adder (a, b, cin, s, cout);
2+
input a;
3+
input b;
4+
input cin;
5+
output s;
6+
output cout;
7+
8+
assign {cout, s} = a + b + cin;
9+
10+
endmodule
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
`timescale 1ns/1ns
2+
3+
module full_adder_tb();
4+
reg a, b, cin;
5+
wire s, cout;
6+
7+
full_adder fa1 (a, b, cin, s, cout);
8+
9+
initial begin
10+
$dumpfile("full_adder_tb.vcd");
11+
$dumpvars(0, full_adder_tb);
12+
13+
a = 0;
14+
b = 0;
15+
cin = 0;
16+
#10
17+
a = 1;
18+
#10
19+
a = 0;
20+
b = 1;
21+
#10
22+
a = 1;
23+
#10
24+
a = 0;
25+
b = 0;
26+
cin = 1;
27+
#10
28+
a = 1;
29+
#10
30+
a = 0;
31+
b = 1;
32+
#10
33+
a = 1;
34+
#10
35+
$finish;
36+
end
37+
38+
always begin
39+
#1 $display("a=%b, b=%b, cin=%b, s=%b, cout=%b", a, b, cin, s, cout);
40+
end
41+
endmodule
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module half_adder_4(a, b, s, cout);
2+
input [3:0] a;
3+
input [3:0] b;
4+
output [3:0] s;
5+
output cout;
6+
7+
assign {cout, s} = a + b;
8+
9+
endmodule
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
`timescale 1ns/1ns
2+
3+
module half_adder_4_tb();
4+
reg [3:0] a, b;
5+
wire [3:0] s;
6+
wire cout;
7+
8+
half_adder_4 ha1 (a, b, s, cout);
9+
10+
initial begin
11+
$dumpfile("half_adder_4_tb.vcd");
12+
$dumpvars(0, half_adder_4_tb);
13+
14+
a = 4'b0000;
15+
b = 4'b0000;
16+
#10
17+
a = 4'b0001;
18+
#10
19+
a = 4'b0000;
20+
b = 4'b0001;
21+
#10
22+
a = 4'b0001;
23+
#10
24+
a = 4'b0010;
25+
b = 4'b0000;
26+
#10
27+
a = 4'b0011;
28+
#10
29+
a = 4'b0100;
30+
b = 4'b0001;
31+
#10
32+
a = 4'b0111;
33+
b = 4'b0011;
34+
#10
35+
a = 4'b1111;
36+
b = 4'b0001;
37+
#10
38+
a = 4'b1111;
39+
b = 4'b0010;
40+
#10
41+
a = 4'b0001;
42+
b = 4'b1111;
43+
#10
44+
a = 4'b0110;
45+
b = 4'b1111;
46+
#10
47+
a = 4'b1111;
48+
b = 4'b1111;
49+
#10
50+
$finish;
51+
end
52+
53+
always begin
54+
#1 $display("a=%b, b=%b, s=%b, cout=%b", a, b, s, cout);
55+
end
56+
endmodule

0 commit comments

Comments
 (0)