forked from hoglet67/verilog-6502
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.v
120 lines (107 loc) · 3.4 KB
/
memory.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
//thanks to https://stackoverflow.com/a/30442903
`default_nettype none
module memory(input wire [20:0] addr,
input wire [7:0] dIn,
output reg [7:0] dOut,
input wire re, we, clk,
input wire CE_n, CER_n);
localparam ROMSIZE = 512*1024;
reg [7:0] ROM[ROMSIZE-1:0];
localparam RAMSIZE = 8*1024;
reg [7:0] RAM[RAMSIZE-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
for(int i = 0; i < RAMSIZE; i++)
RAM[i] = 8'h0;
end
always @(posedge clk) begin
dOut <= 8'hxx;
if(re & we) begin
$display("ERROR! RE AND WE ASSERTED");
#1 $finish;
end
if(addr < 21'h100000) begin //ROM AREA mappings
if(re & ~CE_n) begin
case(romsize)
21'h100000: begin //1MiB ROM
dOut <= ROM[addr[19:0]];
end
21'h80000: begin //512KiB ROM
dOut <= ROM[addr[18:0]];
end
21'h60000: begin //384KiB ROM
if(addr < 21'h80000) //mirror first 256KiB twice
dOut <= ROM[addr[17:0]];
else //mirror last 128KiB every 128KiB
dOut <= ROM[{2'b10, addr[16:0]}];
end
21'h40000: begin //256KiB ROM
dOut <= ROM[addr[17:0]];
end
default: begin //unimplemented ROM size TODO: 768KiB
//$display("ROM SIZE UNIMPLIMENTED!");
dOut <= ROM[addr];
//#1 $finish;
end
endcase
end
end
else if(addr < 21'h1F0000) begin //CD + backup RAM, return 0xFF
dOut <= 8'hFF;
end
else if(addr < 21'h1F8000) begin //RAM on page 0xF8, mirrored 0xF9-0xFB
/*
if(we & ~CER_n)
RAM[addr[12:0]] <= dIn;
else if(re & ~CER_n)
dOut <= RAM[addr[12:0]];
*/
end
else if(addr < 21'hFE000) begin //unused, return 0xFF
dOut <= 8'hFF;
end
else begin //hardware page, just here for debug
if(addr[12:0] < 13'h400) begin
//$display("VDC port %x access", addr[1:0]);
/*if(re & addr[1])
$display("non-status VDC read, this might be bad!");*/
end
else if(addr[12:0] < 13'h800) begin
//$display("VCE port %x access", addr[2:0]);
end
else if(addr[12:0] == 13'h1000) begin // controller
if(re) begin
//$display("controller read");
end
else if(we) begin
//$display("controller write");
end
end
else if(addr[12:0] >= 13'h800 && addr[12:0] < 13'hC00) begin //PSG
//$display("PSG access");
end
else if(addr[12:0] >= 13'hC00 && addr[12:0] < 13'h1000) begin //TIMER
//string str[2] = {"read", "write"};
//$display("TIMER accessed, port %b %s", addr[0], str[~re]);
//$display("dIn: %x", dIn);
end
else begin
if(re) begin
//$display("IO read to %x", addr);
//$display("IO Region Unimplemented");
//#1 $finish;
end
else if(we) begin
//$display("IO write to %x", addr);
end
end
end
end
endmodule