-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaturn_hp48gx_mmio.v
349 lines (301 loc) · 11.2 KB
/
saturn_hp48gx_mmio.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
(c) Raphaël Jacquot 2019
This file is part of hp_saturn.
hp_saturn is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
hp_saturn is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <https://www.gnu.org/licenses/>.
*/
`default_nettype none
`include "saturn_def_buscmd.v"
module saturn_hp48gx_mmio (
i_clk,
i_clk_en,
i_reset,
`ifdef SIM
i_phase,
i_cycle_ctr,
`endif
i_phase_0,
i_debug_cycle,
i_bus_clk_en,
i_bus_is_data,
o_bus_nibble_out,
i_bus_nibble_in,
i_bus_daisy,
o_bus_daisy,
o_bus_active,
o_menu_height,
o_rom_mode
);
input wire [0:0] i_clk;
input wire [0:0] i_clk_en;
input wire [0:0] i_reset;
`ifdef SIM
input wire [1:0] i_phase;
input wire [31:0] i_cycle_ctr;
`endif
input wire [0:0] i_phase_0;
input wire [0:0] i_debug_cycle;
/**************************************************************************************************
*
* bus I/O
*
*************************************************************************************************/
input wire [0:0] i_bus_clk_en;
input wire [0:0] i_bus_is_data;
output reg [3:0] o_bus_nibble_out;
input wire [3:0] i_bus_nibble_in;
input wire [0:0] i_bus_daisy;
output wire [0:0] o_bus_daisy;
output wire [0:0] o_bus_active;
/**************************************************************************************************
*
* I/O registers
*
*************************************************************************************************/
output reg [5:0] o_menu_height; // 0x28 bits 0-3 | 0x29 bits 0-1
output reg [0:0] o_rom_mode; // 0x29 bit 3
/**************************************************************************************************
*
* address handling
*
*************************************************************************************************/
`define MMIO_BITS 6
reg [3:0] ioram_data[0:(2** `MMIO_BITS) - 1];
reg [3:0] last_cmd;
reg [2:0] addr_pos_ctr;
reg [19:0] local_pc;
reg [19:0] local_dp;
reg [0:0] pc_active;
reg [0:0] dp_active;
reg [0:0] configured;
reg [19:0] base_addr;
initial begin
last_cmd = 4'b0;
addr_pos_ctr = 3'b0;
local_pc = 20'b0;
local_dp = 20'b0;
pc_active = 1'b0;
dp_active = 1'b0;
configured = 1'b0;
base_addr = 20'b0;
end
/*
* testing for read
*/
wire [0:0] do_pc_read = (last_cmd == `BUSCMD_PC_READ);
wire [0:0] do_dp_read = (last_cmd == `BUSCMD_DP_READ);
wire [0:0] do_read = do_pc_read || do_dp_read;
/*
* testing for write
*/
wire [0:0] do_pc_write = (last_cmd == `BUSCMD_PC_WRITE);
wire [0:0] do_dp_write = (last_cmd == `BUSCMD_DP_WRITE);
wire [0:0] do_write = do_pc_write || do_dp_write;
/*
* accessing the ioram
*/
assign o_bus_daisy = configured;
wire [0:0] use_pc = do_pc_read || do_pc_write;
wire [0:0] use_dp = do_dp_read || do_dp_write;
wire [19:0] above_addr = base_addr + (2 ** `MMIO_BITS);
wire [0:0] active = ((pc_active && use_pc) || (dp_active && use_dp)) && configured;
assign o_bus_active = active;
wire [19:0] pointer = use_pc?local_pc:local_dp;
wire [19:0] access_pointer = pointer - base_addr;
wire [`MMIO_BITS-1:0] address = access_pointer[`MMIO_BITS-1:0];
wire [0:0] gen_active = i_clk_en && !i_debug_cycle && i_phase_0 && (do_read || do_write);
wire [0:0] can_read = i_bus_clk_en && i_bus_is_data && do_read && active;
wire [0:0] can_write = i_bus_clk_en && i_bus_is_data && do_write && active;
/*
* reading and writing to I/O registers
*/
/*
* generate the active signals
* these comparisons incur important delays
*/
always @(posedge i_clk) begin
if (gen_active) begin
// $display("MMIO-GX %0d: [%d] use_pc %b | use_dp %b | local_pc %5h | local_dp %5h | base %5h | above %5h | conf %b", i_phase, i_cycle_ctr,
// use_pc, use_dp, local_pc, local_dp, base_addr, above_addr, configured);
pc_active <= use_pc && (local_pc >= base_addr) && (local_pc < above_addr) && configured;
dp_active <= use_dp && (local_dp >= base_addr) && (local_dp < above_addr) && configured;
end
if (i_reset) begin
pc_active <= 1'b0;
dp_active <= 1'b0;
end
end
always @(posedge i_clk) begin
if (can_read)
o_bus_nibble_out <= ioram_data[address];
end
reg [0:0] junk_bit_0;
always @(posedge i_clk) begin
if (can_write) begin
case (address)
6'h28: o_menu_height[3:0] <= i_bus_nibble_in;
6'h29: {o_rom_mode, junk_bit_0, o_menu_height[5:4]} <= i_bus_nibble_in;
default:
begin
`ifdef SIM
$display("MMIO-GX %0d: [%d] addr %h not handled", i_phase, i_cycle_ctr, pointer);
`endif
end
endcase
ioram_data[address] <= i_bus_nibble_in;
end
end
`ifdef SIM
wire [3:0] imm_nibble = ioram_data[address];
`endif
/*
* general case
*/
always @(posedge i_clk) begin
if (i_bus_clk_en && i_clk_en) begin
if (i_bus_is_data) begin
/* do things with the bits...*/
case (last_cmd)
`BUSCMD_PC_READ:
begin
// o_bus_nibble_out <= rom_data[local_pc[`ROMBITS-1:0]];
local_pc <= local_pc + 1;
end
`BUSCMD_DP_READ:
begin
// o_bus_nibble_out <= rom_data[local_dp[`ROMBITS-1:0]];
local_dp <= local_dp + 1;
end
`BUSCMD_PC_WRITE: local_pc <= local_pc + 1;
`BUSCMD_DP_WRITE: local_dp <= local_dp + 1;
`BUSCMD_LOAD_PC:
begin
local_pc[addr_pos_ctr*4+:4] <= i_bus_nibble_in;
addr_pos_ctr <= addr_pos_ctr + 3'd1;
end
`BUSCMD_LOAD_DP:
begin
local_dp[addr_pos_ctr*4+:4] <= i_bus_nibble_in;
addr_pos_ctr <= addr_pos_ctr + 3'd1;
end
`BUSCMD_CONFIGURE:
if (i_bus_daisy && !configured) begin
base_addr[addr_pos_ctr*4+:4] <= i_bus_nibble_in;
addr_pos_ctr <= addr_pos_ctr + 3'd1;
end
default: begin end
endcase
/* auto switch to pc read / dp read */
if (addr_pos_ctr == 4) begin
case (last_cmd)
`BUSCMD_LOAD_PC: last_cmd <= `BUSCMD_PC_READ;
`BUSCMD_LOAD_DP: last_cmd <= `BUSCMD_DP_READ;
`BUSCMD_CONFIGURE:
begin
// set above_addr
configured <= 1'b1;
end
default: begin end
endcase
end
`ifdef SIM
$write("MMIO-GX %0d: [%d] ", i_phase, i_cycle_ctr);
case (last_cmd)
`BUSCMD_PC_READ:
begin
$write("PC_READ ");
if (configured)
begin
if (can_read) $write("<= mmio[%5h]: %h", local_pc, imm_nibble);
else $write("inactive");
end
else $write("(unconfigured)");
end
`BUSCMD_DP_READ:
begin
$write("DP_READ ");
if (configured)
begin
if (can_read) $write("<= mmio[%5h]: %h", local_dp, imm_nibble);
else $write("(inactive)");
end
else $write("(unconfigured)");
end
`BUSCMD_DP_WRITE:
begin
$write("DP_WRITE ");
if (configured)
begin
if (can_write) $write("mmio[%5h] <= %h", local_dp, i_bus_nibble_in);
else $write("(inactive %h)", i_bus_nibble_in);
end
else $write("(ignore)");
end
`BUSCMD_LOAD_PC: $write("LOAD_PC - pc %5h, %h pos %0d", local_pc, i_bus_nibble_in, addr_pos_ctr);
`BUSCMD_LOAD_DP: $write("LOAD_DP - dp %5h, %h pos %0d", local_dp, i_bus_nibble_in, addr_pos_ctr);
`BUSCMD_CONFIGURE:
begin
if (!configured) $write("CONFIGURE - base_addr %5h, %h pos %0d", base_addr, i_bus_nibble_in, addr_pos_ctr);
else $write("CONFIGURE - already done, ignore");
end
`BUSCMD_RESET: $write("RESET");
default: $write("last_command %h nibble %h - UNHANDLED", last_cmd, i_bus_nibble_in);
endcase
if (addr_pos_ctr == 4) begin
case (last_cmd)
`BUSCMD_LOAD_PC: $write(" auto switch to PC_READ");
`BUSCMD_LOAD_DP: $write(" auto switch to DP_READ");
default: begin end
endcase
end
$write("\n");
`endif
end else begin
last_cmd <= i_bus_nibble_in;
if ((i_bus_nibble_in == `BUSCMD_LOAD_PC) || (i_bus_nibble_in == `BUSCMD_LOAD_DP))
addr_pos_ctr <= 0;
if (i_bus_nibble_in == `BUSCMD_CONFIGURE)
addr_pos_ctr <= 3'd0;
if (i_bus_nibble_in == `BUSCMD_RESET)
begin
base_addr <= 20'b0;
configured <= 1'b0;
end
`ifdef SIM
$write("MMIO-GX %0d: [%d] ", i_phase, i_cycle_ctr);
case (i_bus_nibble_in)
`BUSCMD_PC_READ: $write("PC_READ");
`BUSCMD_DP_READ: $write("DP_READ");
`BUSCMD_DP_WRITE: $write("DP_WRITE");
`BUSCMD_LOAD_PC: $write("LOAD_PC");
`BUSCMD_LOAD_DP: $write("LOAD_DP");
`BUSCMD_CONFIGURE: $write("CONFIGURE");
`BUSCMD_RESET: $write("RESET base_addr to %5h and unconfigure", 20'h0);
default: begin end
endcase
$write("\n");
`endif
end
end
if (i_reset) begin
last_cmd <= 4'b0;
addr_pos_ctr <= 3'b0;
local_pc <= 20'b0;
local_dp <= 20'b0;
configured <= 1'b0;
base_addr <= 20'b0;
end
end
// Verilator lint_off UNUSED
wire [(20 -`MMIO_BITS):0] unused;
assign unused = { junk_bit_0, access_pointer[19:`MMIO_BITS] };
// Verilator lint_on UNUSED
endmodule