-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.v
153 lines (128 loc) · 3.61 KB
/
lib.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
module invert (input wire i, output wire o);
assign o = !i;
endmodule
module and2 (input wire i0, i1, output wire o);
assign o = i0 & i1;
endmodule
module or2 (input wire i0, i1, output wire o);
assign o = i0 | i1;
endmodule
module xor2 (input wire i0, i1, output wire o);
assign o = i0 ^ i1;
endmodule
module nand2 (input wire i0, i1, output wire o);
wire t;
and2 and2_0 (i0, i1, t);
invert invert_0 (t, o);
endmodule
module nor2 (input wire i0, i1, output wire o);
wire t;
or2 or2_0 (i0, i1, t);
invert invert_0 (t, o);
endmodule
module xnor2 (input wire i0, i1, output wire o);
wire t;
xor2 xor2_0 (i0, i1, t);
invert invert_0 (t, o);
endmodule
module and3 (input wire i0, i1, i2, output wire o);
wire t;
and2 and2_0 (i0, i1, t);
and2 and2_1 (i2, t, o);
endmodule
module or3 (input wire i0, i1, i2, output wire o);
wire t;
or2 or2_0 (i0, i1, t);
or2 or2_1 (i2, t, o);
endmodule
module nor3 (input wire i0, i1, i2, output wire o);
wire t;
or2 or2_0 (i0, i1, t);
nor2 nor2_0 (i2, t, o);
endmodule
module nand3 (input wire i0, i1, i2, output wire o);
wire t;
and2 and2_0 (i0, i1, t);
nand2 nand2_1 (i2, t, o);
endmodule
module xor3 (input wire i0, i1, i2, output wire o);
wire t;
xor2 xor2_0 (i0, i1, t);
xor2 xor2_1 (i2, t, o);
endmodule
module xnor3 (input wire i0, i1, i2, output wire o);
wire t;
xor2 xor2_0 (i0, i1, t);
xnor2 xnor2_0 (i2, t, o);
endmodule
module mux2 (input wire i0, i1, j, output wire o);
assign o = (j==0)?i0:i1;
endmodule
module mux4 (input wire [0:3] i, input wire j1, j0, output wire o);
wire t0, t1;
mux2 mux2_0 (i[0], i[1], j1, t0);
mux2 mux2_1 (i[2], i[3], j1, t1);
mux2 mux2_2 (t0, t1, j0, o);
endmodule
module mux8 (input wire [0:7] i, input wire j2, j1, j0, output wire o);
wire t0, t1;
mux4 mux4_0 (i[0:3], j2, j1, t0);
mux4 mux4_1 (i[4:7], j2, j1, t1);
mux2 mux2_0 (t0, t1, j0, o);
endmodule
module demux2 (input wire i, j, output wire o0, o1);
assign o0 = (j==0)?i:1'b0;
assign o1 = (j==1)?i:1'b0;
endmodule
module demux4 (input wire i, j1, j0, output wire [0:3] o);
wire t0, t1;
demux2 demux2_0 (i, j1, t0, t1);
demux2 demux2_1 (t0, j0, o[0], o[1]);
demux2 demux2_2 (t1, j0, o[2], o[3]);
endmodule
module demux8 (input wire i, j2, j1, j0, output wire [0:7] o);
wire t0, t1;
demux2 demux2_0 (i, j2, t0, t1);
demux4 demux4_0 (t0, j1, j0, o[0:3]);
demux4 demux4_1 (t1, j1, j0, o[4:7]);
endmodule
module df (input wire clk, in, output wire out);
reg df_out;
always@(posedge clk) df_out <= in;
assign out = df_out;
endmodule
module dfr (input wire clk, reset, in, output wire out);
wire reset_, df_in;
invert invert_0 (reset, reset_);
and2 and2_0 (in, reset_, df_in);
df df_0 (clk, df_in, out);
endmodule
module dfrl (input wire clk, reset, load, in, output wire out);
wire _in;
mux2 mux2_0(out, in, load, _in);
dfr dfr_1(clk, reset, _in, out);
endmodule
module dfs (input wire clk, set, in, output wire out);
wire dfr_in,dfr_out;
invert invert_0(in, dfr_in);
invert invert_1(dfr_out, out);
dfr dfr_2(clk, set, dfr_in, dfr_out);
endmodule
module dfsl (input wire clk, set, load, in, output wire out);
wire _in;
mux2 mux2_0(out, in, load, _in);
dfs dfs_1(clk, set, _in, out);
endmodule
module fa (input wire i0, i1, cin, output wire sum, cout);
wire t0, t1, t2;
xor3 _i0 (i0, i1, cin, sum);
and2 _i1 (i0, i1, t0);
and2 _i2 (i1, cin, t1);
and2 _i3 (cin, i0, t2);
or3 _i4 (t0, t1, t2, cout);
endmodule
module addsub (input wire addsub, i0, i1, cin, output wire sumdiff, cout);
wire t;
fa _i0 (i0, t, cin, sumdiff, cout);
xor2 _i1 (i1, addsub, t);
endmodule