-
Notifications
You must be signed in to change notification settings - Fork 0
/
apb_master.v
158 lines (146 loc) · 2.5 KB
/
apb_master.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
154
155
156
157
158
`define PERIOD 10
module apb_master (
pclk,
rst_n,
paddr,
pwrite,
psel,
penable,
pwdata,
prdata,
pready);
input pclk;
output reg rst_n;
output reg [3:0] paddr;
output reg pwrite;
output reg psel;
output reg penable;
output reg [7:0] pwdata;
input reg [7:0] prdata;
input pready;
task wait4clk(integer n);
begin
repeat (n)
@(posedge pclk);
end
endtask
task reset;
begin
rst_n = 0;
wait4clk(4);
rst_n = 1;
wait4clk(2);
end
endtask
task apb_write(input [3:0] addr, input [7:0] data);
begin
pwrite = 1;
psel = 1;
penable = 0;
paddr = addr;
pwdata = data;
wait4clk(1);
penable = 1;
wait4clk(1);
while (!pready)
wait4clk(1);
psel = 0;
penable = 0;
end
endtask
task apb_read(input [3:0] addr);
begin
pwrite = 0;
psel = 1;
penable = 0;
paddr = addr;
wait4clk(1);
penable = 1;
wait4clk(1);
while (!pready)
wait4clk(1);
`ifdef USE_RAM_MODEL
wait4clk(1);
`endif
$display("Read data: %d", prdata);
penable = 0;
psel = 0;
end
endtask
endmodule
module tb();
logic clk, sclk;
logic rst_n;
reg [3:0] paddr;
logic pwrite;
logic psel;
logic penable;
reg [7:0] pwdata;
reg [7:0] prdata;
logic pready;
initial begin
clk = 0;
forever
#(`PERIOD/2) clk = ~clk;
end
assign #0 sclk = clk;
apb_master apb_m(
.pclk(clk),
.rst_n(rst_n),
.paddr(paddr),
.pwrite(pwrite),
.psel(psel),
.penable(penable),
.pwdata(pwdata),
.prdata(prdata),
.pready(pready)
);
apb_slave apb_s(
.pclk(sclk),
.rst_n(rst_n),
.paddr(paddr),
.pwrite(pwrite),
.psel(psel),
.penable(penable),
.pwdata(pwdata),
.prdata(prdata),
.pready(pready)
);
initial begin
$dumpfile("tb.vcd");
$dumpvars(0,tb);
apb_m.reset();
apb_m.wait4clk(2);
apb_m.apb_write(2,5);
`ifdef GAPCLK
apb_m.wait4clk(1);
`endif
apb_m.apb_write(3,10);
`ifdef GAPCLK
apb_m.wait4clk(1);
`endif
apb_m.apb_write(4,5);
`ifdef GAPCLK
apb_m.wait4clk(1);
`endif
apb_m.apb_write(5,10);
`ifdef GAPCLK
apb_m.wait4clk(1);
`endif
apb_m.apb_read(2);
`ifdef GAPCLK
apb_m.wait4clk(1);
`endif
apb_m.apb_read(3);
`ifdef GAPCLK
apb_m.wait4clk(1);
`endif
apb_m.apb_read(4);
`ifdef GAPCLK
apb_m.wait4clk(1);
`endif
apb_m.apb_read(5);
apb_m.wait4clk(2);
$finish;
end
endmodule