forked from hoglet67/verilog-6502
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathROM.v
106 lines (94 loc) · 2.6 KB
/
ROM.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
`default_nettype none
//`define EMULATE_ROM
/**
* PC Engine ROM (HuCard/TurboChip) emulator
* No support for on-card memory mappers or additional RAM
*/
module ROM(input wire clk, reset,
input wire [19:0] addr,
input wire [2:0] SW,
output wire [19:0] SRAM_ADDR,
input wire [15:0] SRAM_DQ,
inout wire [7:0] D,
input wire RD_n, CE_n);
reg [2:0] romsel;
reg [19:0] mapped_addr;
wire [7:0] dOut;
assign D = (~RD_n & ~CE_n) ? dOut : 8'bz;
always @* begin
mapped_addr = 20'hx;
case(romsel) //ROM mappings
3'b011: begin //1MiB ROM
mapped_addr = addr;
end
3'b010: begin //512KiB ROM
mapped_addr = addr[18:0];
end
3'b001: begin //384KiB ROM
if(addr < 21'h80000) //mirror first 256KiB twice
mapped_addr = addr[17:0];
else //mirror last 128KiB every 128KiB
mapped_addr = {2'b10, addr[16:0]};
end
3'b000: begin //256KiB ROM
mapped_addr = addr[17:0];
end
default: begin //unimplemented ROM size TODO: 768KiB
mapped_addr = addr;
//#10 $display("ROM SIZE UNIMPLIMENTED!");
//#1 $finish;
end
endcase
end
`ifdef EMULATE_ROM
localparam ROMSIZE = 512*1024;
reg [7:0] ROM[ROMSIZE-1:0];
int romsize;
initial begin
$readmemh("PRG.hex", ROM);
//hacky way to autodetect ROM size for testbench
for(romsize = 0; romsize < ROMSIZE; romsize++)
if(^ROM[romsize] === 1'bx) begin
$display("romsize: %dk", romsize/1024);
break;
end
end
assign dOut = ROM[mapped_addr];
always @* begin
romsel = 0;
case(romsize) //ROM mappings
21'h100000: begin //1MiB ROM
romsel = 3'b011;
end
21'h80000: begin //512KiB ROM
romsel = 3'b010;
end
21'h60000: begin //384KiB ROM
romsel = 3'b001;
end
21'h40000: begin //256KiB ROM
romsel = 3'b000;
end
default: begin //unimplemented ROM size TODO: 768KiB
romsel = 3'b111;
end
endcase
end
`else // !`ifdef EMULATE_ROM
//logic [19:0] sram_addr_reg;
always_ff @(posedge clk) begin
if(reset)
romsel <= SW;
end
/*
always_comb begin
if(addr < 21'h80000) //mirror first 256KiB twice
sram_addr_reg = addr[17:0];
else //mirror last 128KiB every 128KiB
sram_addr_reg = {2'b10, addr[16:0]};
end
*/
assign SRAM_ADDR = {1'b0, mapped_addr[19:1]};
assign dOut = (mapped_addr[0]) ? SRAM_DQ[15:8] : SRAM_DQ[7:0];
`endif // `ifdef EMULATE_ROM
endmodule