1 |
3 |
hharte |
//////////////////////////////////////////////////////////////////////
|
2 |
|
|
//// ////
|
3 |
4 |
hharte |
//// $Id: wb_mmu.v,v 1.2 2008-12-02 15:15:37 hharte Exp $ ////
|
4 |
3 |
hharte |
//// wb_mmu.v - Simple Memory Mapping Unit with Wishbone ////
|
5 |
|
|
//// Slave interface for configuration. ////
|
6 |
|
|
//// ////
|
7 |
|
|
//// This file is part of the Vector Graphic Z80 SBC Project ////
|
8 |
|
|
//// http://www.opencores.org/projects/vg_z80_sbc/ ////
|
9 |
|
|
//// ////
|
10 |
|
|
//// Author: ////
|
11 |
|
|
//// - Howard M. Harte (hharte@opencores.org) ////
|
12 |
|
|
//// ////
|
13 |
|
|
//////////////////////////////////////////////////////////////////////
|
14 |
|
|
//// ////
|
15 |
|
|
//// Copyright (C) 2008 Howard M. Harte ////
|
16 |
|
|
//// ////
|
17 |
|
|
//// This source file may be used and distributed without ////
|
18 |
|
|
//// restriction provided that this copyright statement is not ////
|
19 |
|
|
//// removed from the file and that any derivative work contains ////
|
20 |
|
|
//// the original copyright notice and the associated disclaimer. ////
|
21 |
|
|
//// ////
|
22 |
|
|
//// This source file is free software; you can redistribute it ////
|
23 |
|
|
//// and/or modify it under the terms of the GNU Lesser General ////
|
24 |
|
|
//// Public License as published by the Free Software Foundation; ////
|
25 |
|
|
//// either version 2.1 of the License, or (at your option) any ////
|
26 |
|
|
//// later version. ////
|
27 |
|
|
//// ////
|
28 |
|
|
//// This source is distributed in the hope that it will be ////
|
29 |
|
|
//// useful, but WITHOUT ANY WARRANTY; without even the implied ////
|
30 |
|
|
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ////
|
31 |
|
|
//// PURPOSE. See the GNU Lesser General Public License for more ////
|
32 |
|
|
//// details. ////
|
33 |
|
|
//// ////
|
34 |
|
|
//// You should have received a copy of the GNU Lesser General ////
|
35 |
|
|
//// Public License along with this source; if not, download it ////
|
36 |
|
|
//// from http://www.opencores.org/lgpl.shtml ////
|
37 |
|
|
//// ////
|
38 |
|
|
//////////////////////////////////////////////////////////////////////
|
39 |
|
|
|
40 |
|
|
//+---------------------------------------------------------------------------+
|
41 |
|
|
//|
|
42 |
|
|
//| Simple Memory Mapping Unit (MMU) for allowing a CPU with a 16-bit address
|
43 |
|
|
//| space to access a 16MB address space, using 16 4K pages.
|
44 |
|
|
//|
|
45 |
|
|
//| The MMU has a table of 16 4K "pages" that can be map an address in the
|
46 |
|
|
//| 64K space into a corresponding address in a 16MB space. Each 4K page can
|
47 |
|
|
//| map to any 4K boundary in a 16MB (24-bit) physical address space.
|
48 |
|
|
//|
|
49 |
|
|
//| The MMU occupies four byte-wide memory locations, and must be accessed as
|
50 |
|
|
//| bytes. The registers are as follows:
|
51 |
|
|
//|
|
52 |
|
|
//| 0 - MMR_L Lower 8-bits = MMR_L <ll>
|
53 |
|
|
//| 1 - MMR_H Upper 4-bits = MMR_H <h>
|
54 |
|
|
//| 2 - ADR_INDEX (0x0-0xF to select the memory region modified by the MMR_x
|
55 |
|
|
//| registers. This register defaults to 0 at reset.
|
56 |
|
|
//| 3 - LOCK Writing 0xA5 to this register unlocks to MMU, any other
|
57 |
|
|
//| value locks it. When locked, the MMR_L, MMR_H registers
|
58 |
|
|
//| are read-only. Reading the LOCK register returns 0x51
|
59 |
|
|
//| if locked, 0x50 if unlocked. The MMU is locked on reset.
|
60 |
|
|
//|
|
61 |
|
|
//| The MMU forms the final 24-bit address on the 4K page as follows:
|
62 |
|
|
//|
|
63 |
|
|
//| <pxxx> - 64K unmapped address (mmu_adr_i)
|
64 |
|
|
//| <h>:<llxxx> - 16M mapped address (mmu_adr_o)
|
65 |
|
|
//|
|
66 |
|
|
//| where: p = 4K page in 64K address space
|
67 |
|
|
//| h = MMR_H register
|
68 |
|
|
//| l = MMR_L register
|
69 |
|
|
//| x = address bits passed through the MMU unchanged.
|
70 |
|
|
//|
|
71 |
|
|
//+---------------------------------------------------------------------------+
|
72 |
|
|
module wb_mmu(
|
73 |
|
|
clk_i, nrst_i, wbs_adr_i, wbs_dat_o, wbs_dat_i, wbs_sel_i, wbs_we_i,
|
74 |
|
|
wbs_stb_i, wbs_cyc_i, wbs_ack_o,
|
75 |
|
|
mmu_adr_i,
|
76 |
|
|
mmu_adr_o
|
77 |
|
|
);
|
78 |
|
|
|
79 |
|
|
//
|
80 |
|
|
// Default address and data bus width
|
81 |
|
|
//
|
82 |
|
|
parameter aw = 10; //number of address-bits
|
83 |
|
|
parameter dw = 32; //number of data-bits
|
84 |
|
|
|
85 |
|
|
// Wishbone Slave Interface
|
86 |
4 |
hharte |
input clk_i;
|
87 |
|
|
input nrst_i;
|
88 |
|
|
input [aw-1:0] wbs_adr_i;
|
89 |
|
|
output reg [dw-1:0] wbs_dat_o;
|
90 |
|
|
input [dw-1:0] wbs_dat_i;
|
91 |
|
|
input [3:0] wbs_sel_i;
|
92 |
|
|
input wbs_we_i;
|
93 |
|
|
input wbs_stb_i;
|
94 |
|
|
input wbs_cyc_i;
|
95 |
|
|
output reg wbs_ack_o;
|
96 |
3 |
hharte |
|
97 |
|
|
// MMU Address Interface
|
98 |
|
|
output [23:0] mmu_adr_o;
|
99 |
|
|
input [23:0] mmu_adr_i;
|
100 |
|
|
|
101 |
|
|
// Internal storage for mapping and state information
|
102 |
4 |
hharte |
reg [11:0] mmu_lut[0:15];
|
103 |
|
|
reg [3:0] adr_index;
|
104 |
|
|
reg mmu_lock;
|
105 |
3 |
hharte |
|
106 |
|
|
//
|
107 |
|
|
// generate wishbone register bank writes
|
108 |
|
|
wire wbs_acc = wbs_cyc_i & wbs_stb_i; // WISHBONE access
|
109 |
|
|
wire wbs_wr = wbs_acc & wbs_we_i; // WISHBONE write access
|
110 |
|
|
wire wbs_rd = wbs_acc & !wbs_we_i; // WISHBONE read access
|
111 |
4 |
hharte |
reg [4:0] i;
|
112 |
3 |
hharte |
|
113 |
|
|
always @(posedge clk_i or negedge nrst_i)
|
114 |
4 |
hharte |
if(~nrst_i) // Reset
|
115 |
3 |
hharte |
begin
|
116 |
|
|
wbs_ack_o <= 1'b0;
|
117 |
|
|
adr_index <= 4'b0;
|
118 |
|
|
mmu_lock <= 1'b1; // Lock MMU on reset
|
119 |
4 |
hharte |
|
120 |
3 |
hharte |
// Initial values for MMU mapping table.
|
121 |
4 |
hharte |
mmu_lut[0] <= 12'h101; // 0x0xxx - Shadow of Monitor, only used to jump to monitor at 0xE000.
|
122 |
3 |
hharte |
// But not the same copy as at E000, because the init patches RST38.
|
123 |
4 |
hharte |
mmu_lut[1] <= 12'h102; // 0x1000
|
124 |
|
|
mmu_lut[2] <= 12'h103; // 0x2000
|
125 |
|
|
mmu_lut[3] <= 12'h101; // 0x3000
|
126 |
|
|
mmu_lut[4] <= 12'h102; // 0x4000
|
127 |
|
|
mmu_lut[5] <= 12'h103; // 0x5000
|
128 |
|
|
mmu_lut[6] <= 12'h101; // 0x6000
|
129 |
|
|
mmu_lut[7] <= 12'h102; // 0x7000
|
130 |
|
|
mmu_lut[8] <= 12'h803; // 0x8000
|
131 |
|
|
mmu_lut[9] <= 12'h809; // 0x9000
|
132 |
|
|
mmu_lut[10] <= 12'h80A; // 0xA000
|
133 |
|
|
mmu_lut[11] <= 12'h80B; // 0xB000
|
134 |
|
|
mmu_lut[12] <= 12'h200; // 0xC000 - FLASH (ZMON)
|
135 |
|
|
mmu_lut[13] <= 12'hF02; // 0xD000 - MMU
|
136 |
|
|
mmu_lut[14] <= 12'h100; // 0xE000 - FLASH (MON4.3)
|
137 |
|
|
mmu_lut[15] <= 12'h600; // 0xF000 - VGA
|
138 |
3 |
hharte |
end
|
139 |
|
|
else begin
|
140 |
|
|
if(wbs_wr) // Wishbone Write, decode byte enables to determine register offset.
|
141 |
|
|
case(wbs_sel_i)
|
142 |
4 |
hharte |
4'b0001: begin // Data L Register
|
143 |
3 |
hharte |
if(mmu_lock == 1'b0)
|
144 |
|
|
mmu_lut[adr_index[3:0]][7:0] <= wbs_dat_i[7:0];
|
145 |
|
|
end
|
146 |
4 |
hharte |
4'b0010: begin // Data H Register
|
147 |
3 |
hharte |
if(mmu_lock == 1'b0)
|
148 |
|
|
mmu_lut[adr_index[3:0]][11:8] <= wbs_dat_i[11:8];
|
149 |
|
|
end
|
150 |
4 |
hharte |
4'b0100: begin // Index Register
|
151 |
3 |
hharte |
adr_index <= wbs_dat_i[19:16];
|
152 |
|
|
end
|
153 |
4 |
hharte |
4'b1000: begin // Lock Register
|
154 |
|
|
if(wbs_dat_i[31:24] == 8'hA5) begin
|
155 |
3 |
hharte |
mmu_lock <= 1'b0;
|
156 |
|
|
end else begin
|
157 |
|
|
mmu_lock <= 1'b1;
|
158 |
|
|
end
|
159 |
|
|
end
|
160 |
|
|
endcase
|
161 |
|
|
|
162 |
|
|
if(wbs_rd) begin
|
163 |
|
|
case(wbs_sel_i) // Wishbone Read, decode byte enables to determine register offset.
|
164 |
4 |
hharte |
4'b0001: begin // Data L Register
|
165 |
3 |
hharte |
wbs_dat_o <= {20'b0, mmu_lut[adr_index[3:0]]};
|
166 |
|
|
end
|
167 |
4 |
hharte |
4'b0010: begin // Data H Register
|
168 |
3 |
hharte |
wbs_dat_o <= {20'b0, mmu_lut[adr_index[3:0]]};
|
169 |
|
|
end
|
170 |
4 |
hharte |
4'b0100: begin // Index Register
|
171 |
3 |
hharte |
wbs_dat_o <= {20'b0, adr_index, 8'b0};
|
172 |
|
|
end
|
173 |
4 |
hharte |
4'b1000: begin // Lock Register
|
174 |
3 |
hharte |
wbs_dat_o <= {4'h5, 3'b0, mmu_lock, 24'b0};
|
175 |
|
|
end
|
176 |
|
|
endcase
|
177 |
|
|
end
|
178 |
|
|
|
179 |
|
|
wbs_ack_o <= #1 wbs_acc & !wbs_ack_o;
|
180 |
|
|
end
|
181 |
|
|
|
182 |
|
|
// Make the address mapping based on the MMU input address.
|
183 |
|
|
wire [11:0] mmu_out = { mmu_lut[mmu_adr_i[15:12]] };
|
184 |
|
|
|
185 |
|
|
// Output the mapped address with the lower 12-bits passed through.
|
186 |
|
|
assign mmu_adr_o = {mmu_out[11:0], mmu_adr_i[11:0]};
|
187 |
|
|
|
188 |
|
|
endmodule
|
189 |
|
|
|
190 |
|
|
|