GR8RAM/cpld/GR8RAM.v

227 lines
7.6 KiB
Coq
Raw Normal View History

module GR8RAM(C7M, C7M_2, Q3, PHI0in, PHI1in, nRES, nMode,
A, RA, nWE, D, RD,
nDEVSEL, nIOSEL, nIOSTRB, nINH,
2019-09-05 01:45:56 +00:00
nRAS, nCAS0, nCAS1, nRCS, nROE, nRWE);
2019-09-01 02:55:04 +00:00
/* Clock, Reset, Mode */
input C7M, C7M_2, Q3, PHI0in, PHI1in; // Clock inputs
input nRES, nMode; // Reset, mode
2019-09-01 02:55:04 +00:00
/* PHI1 Delay */
2020-01-26 20:13:37 +00:00
wire [8:0] PHI1b; wire PHI1;
2019-09-01 02:55:04 +00:00
LCELL PHI1b0_MC (.in(PHI1in), .out(PHI1b[0]));
LCELL PHI1b1_MC (.in(PHI1b[0]), .out(PHI1b[1]));
LCELL PHI1b2_MC (.in(PHI1b[1]), .out(PHI1b[2]));
LCELL PHI1b3_MC (.in(PHI1b[2]), .out(PHI1b[3]));
LCELL PHI1b4_MC (.in(PHI1b[3]), .out(PHI1b[4]));
LCELL PHI1b5_MC (.in(PHI1b[4]), .out(PHI1b[5]));
LCELL PHI1b6_MC (.in(PHI1b[5]), .out(PHI1b[6]));
LCELL PHI1b7_MC (.in(PHI1b[6]), .out(PHI1b[7]));
LCELL PHI1b8_MC (.in(PHI1b[7]), .out(PHI1b[8]));
LCELL PHI1b9_MC (.in(PHI1b[8] & PHI1in), .out(PHI1));
2019-09-01 02:55:04 +00:00
/* Address Bus, etc. */
input nDEVSEL, nIOSEL, nIOSTRB; // Card select signals
input [15:0] A; // 6502 address bus
input nWE; // 6502 R/W
output [10:0] RA; // DRAM/ROM address
assign RA[10:8] = ASel ? Addr[21:19] : Addr[10:8];
2019-09-03 00:56:37 +00:00
assign RA[7:0] = (~nIOSTRB & FullIOEN) ? Bank+1 :
2019-09-05 01:45:56 +00:00
(~nIOSTRB & ~FullIOEN) ? {7'b0000001, Bank[0]} :
(~ASel & nIOSEL & nIOSTRB) ? Addr[18:11] :
(ASel & nIOSEL & nIOSTRB) ? Addr[7:0] : 8'h00;
2019-09-01 02:55:04 +00:00
/* Select Signals */
wire BankSELA = A[3:0]==4'hF;
wire SetSELA = A[3:0]==4'hE;
wire RAMSELA = A[3:0]==4'h3;
wire AddrHSELA = A[3:0]==4'h2;
wire AddrMSELA = A[3:0]==4'h1;
wire AddrLSELA = A[3:0]==4'h0;
LCELL BankWR_MC (.in(BankSELA & ~nWE & ~nDEVSEL & REGEN), .out(BankWR)); wire BankWR;
2019-10-21 02:41:24 +00:00
LCELL SetWR_MC (.in(SetSELA & ~nWE & ~nDEVSEL & REGEN), .out(SetWR)); wire SetWR;
LCELL RAMSEL_MC (.in(RAMSELA & ~nDEVSEL & REGEN), .out(RAMSEL)); wire RAMSEL;
LCELL AddrHWR_MC (.in(AddrHSELA & ~nWE & ~nDEVSEL & REGEN), .out(AddrHWR)); wire AddrHWR;
LCELL AddrMWR_MC (.in(AddrMSELA & ~nWE & ~nDEVSEL & REGEN), .out(AddrMWR)); wire AddrMWR;
LCELL AddrLWR_MC (.in(AddrLSELA & ~nWE & ~nDEVSEL & REGEN), .out(AddrLWR)); wire AddrLWR;
2019-09-01 02:55:04 +00:00
/* Data Bus Routing */
// DRAM/ROM data bus
2020-01-26 20:13:37 +00:00
wire RDOE = DBEN & ~nWE;
2019-09-01 02:55:04 +00:00
inout [7:0] RD = RDOE ? D[7:0] : 8'bZ;
// Apple II data bus
2020-01-26 20:13:37 +00:00
wire DOE = DBEN & nWE &
2019-09-01 02:55:04 +00:00
((~nDEVSEL & REGEN) | ~nIOSEL | (~nIOSTRB & IOROMEN));
wire [7:0] Dout = (nDEVSEL | RAMSELA) ? RD[7:0] :
2019-10-12 00:34:51 +00:00
AddrHSELA ? {Addr[23:16]} :
2019-09-01 02:55:04 +00:00
AddrMSELA ? Addr[15:8] :
AddrLSELA ? Addr[7:0] : 8'h00;
inout [7:0] D = DOE ? Dout : 8'bZ;
/* Inhibit output */
2019-12-21 06:46:05 +00:00
output nINH = 1'bZ;
2019-09-01 02:55:04 +00:00
/* DRAM and ROM Control Signals */
2020-01-26 20:13:37 +00:00
output nRCS = ~((~nIOSEL | (~nIOSTRB & IOROMEN)) & CSDBEN & nRES); // ROM chip select
2019-09-01 02:55:04 +00:00
output nROE = ~nWE; // need this for flash ROM
2020-01-26 20:13:37 +00:00
output reg nRWE; // for ROM & DRAM
2019-09-01 02:55:04 +00:00
output nRAS = ~(RASr | RASf);
output nCAS0 = ~(CAS0f | (CASr & RAMSEL & ~Addr[22])); // DRAM CAS bank 0
output nCAS1 = ~(CAS1f | (CASr & RAMSEL & Addr[22])); // DRAM CAS bank 1
2019-09-01 02:55:04 +00:00
/* 6502-accessible Registers */
reg [7:0] Bank = 0; // Bank register for ROM access
reg [23:0] Addr = 0; // RAM address register
2019-09-05 01:45:56 +00:00
/* Increment Control */
reg IncAddrL = 0, IncAddrM = 0, IncAddrH = 0;
2019-09-01 02:55:04 +00:00
/* CAS rising/falling edge components */
2019-10-21 02:41:24 +00:00
// These are combined to create the CAS outputs.
reg CASr = 0, CAS0f = 0, CAS1f = 0;
reg RASr = 0, RASf = 0;
2020-01-26 20:13:37 +00:00
reg ASel = 0; // DRAM address multiplexer select
2019-09-01 02:55:04 +00:00
/* State Counters */
reg PHI1reg = 0; // Saved PHI1 at last rising clock edge
reg PHI0seen = 0; // Have we seen PHI0 since reset?
reg [2:0] S = 0; // State counter
reg [3:0] Ref = 0; // Refresh skip counter
2019-09-01 02:55:04 +00:00
/* Misc. */
reg REGEN = 0; // Register enable
reg IOROMEN = 0; // IOSTRB ROM enable
2020-01-26 20:13:37 +00:00
reg FullIOEN = 0; // Set to enable full IOROM space
reg DBEN = 0; // data bus driver gating
reg RDCSEN = 0; // ROM CS enable for reads
reg WRCSEN = 0; // ROM CS gating for writes
2019-09-01 02:55:04 +00:00
// Apple II Bus Compatibiltiy Rules:
// Synchronize to PHI0 or PHI1. (PHI1 here)
// PHI1's edge may be -20ns,+10ns relative to C7M.
// Delay the rising edge of PHI1 to get enough hold time:
// PHI1modified = PHI1 & PHI1delayed;
// Only sample /DEVSEL, /IOSEL at these times:
// 2nd and 3rd rising edge of C7M in PHI0 (S4, S5)
// all 3 falling edges of C7M in PHI0 (S4, S5, S6)
// Can sample /IOSTRB at same times as /IOSEL, plus:
// 1st rising edge of C7M in PHI0 (S3)
2020-01-26 20:13:37 +00:00
always @(posedge C7M) begin
2019-12-21 06:46:05 +00:00
// Synchronize state counter to S1 when just entering PHI1
PHI1reg <= PHI1; // Save old PHI1
if (~PHI1) PHI0seen <= 1; // PHI0seen set in PHI0
S <= (PHI1 & ~PHI1reg & PHI0seen) ? 4'h1 :
S==0 ? 3'h0 :
S==7 ? 3'h7 : S+1;
// Refresh counter allows DRAM refresh once every 13 cycles
if (S==3) Ref <= (Ref[3:2]==2'b11) ? 4'h0 : Ref+1;
// Only drive Apple II data bus after state 4 to avoid bus fight.
// Thus we wait 1.5 7M cycles (210 ns) into PHI0 before driving.
// Same for driving the ROM/SRAM data bus (RD).
2020-01-26 20:13:37 +00:00
DBEN <= S==4 | S==5 | S==6 | S==7;
// Similarly, only select the ROM chip starting at
// the end of S4 for reads and the end of S5 for writes.
// This ensures that write data is valid for
// the entire time that the ROM is selected,
// and minimizes power consumption for reads.
RDCSEN <= S==4 | S==5 | S==6 | S==7;
WRCSEN <= S==5 | S==6 | S==7;
2019-12-21 06:46:05 +00:00
end
always @(posedge C7M, negedge nRES) begin
if (~nRES) begin
REGEN <= 0;
IOROMEN <= 0;
2019-09-01 02:55:04 +00:00
end else begin
// Synchronize state counter to S1 when just entering PHI1
PHI1reg <= PHI1; // Save old PHI1
if (~PHI1) PHI0seen <= 1; // PHI0seen set in PHI0
S <= (PHI1 & ~PHI1reg & PHI0seen) ? 4'h1 :
S==0 ? 3'h0 :
S==7 ? 3'h7 : S+1;
// Refresh counter allows DRAM refresh once every 13 cycles
2019-10-21 02:41:24 +00:00
if (S==3) Ref <= (Ref[3:2] == 2'b11) ? 4'h0 : Ref+1;
2019-09-01 02:55:04 +00:00
// Disable IOSTRB ROM when accessing 0xCFFF.
if (S==3 & ~nIOSTRB & A[10:0]==11'h7FF) IOROMEN <= 1'b0;
// Registers enabled at end of S4 by any IOSEL access (Cn00-CnFF).
if (S==4 & ~nIOSEL) REGEN <= 1;
// Enable IOSTRB ROM when accessing CnXX in IOSEL ROM.
if (S==4 & ~nIOSEL) IOROMEN <= 1'b1;
2019-09-05 01:45:56 +00:00
end
end
always @(negedge C7M, negedge nRES) begin
if (~nRES) begin
Addr <= 0;
Bank <= 0;
FullIOEN <= 0;
IncAddrL <= 0;
IncAddrM <= 0;
IncAddrH <= 0;
2019-09-05 01:45:56 +00:00
end else begin
// Increment address register
if (S==1 & IncAddrL) begin
Addr[7:0] <= Addr[7:0]+1;
IncAddrL <= 0;
IncAddrM <= Addr[7:0] == 8'hFF;
end
if (S==2 & IncAddrM) begin
Addr[15:8] <= Addr[15:8]+1;
IncAddrM <= 0;
IncAddrH <= Addr[15:8] == 8'hFF;
end
if (S==3 & IncAddrH) begin
IncAddrH <= 0;
2019-10-12 00:34:51 +00:00
Addr[23:16] <= Addr[23:16]+1;
end
// Set register in middle of S6 if accessed.
if (S==6) begin
if (BankWR) Bank[7:0] <= D[7:0]; // Bank
2019-09-03 00:56:37 +00:00
if (SetWR) FullIOEN <= D[7:0] == 8'hE5;
2019-09-05 01:45:56 +00:00
IncAddrL <= RAMSEL;
IncAddrM <= AddrLWR & Addr[7] & ~D[7];
IncAddrH <= AddrMWR & Addr[15] & ~D[7];
2019-10-12 00:34:51 +00:00
if (AddrHWR) Addr[23:16] <= D[7:0]; // Addr hi
2019-09-01 02:55:04 +00:00
if (AddrMWR) Addr[15:8] <= D[7:0]; // Addr mid
if (AddrLWR) Addr[7:0] <= D[7:0]; // Addr lo
end
end
end
/* DRAM RAS/CAS */
2020-01-26 20:13:37 +00:00
always @(posedge C7M) begin
2019-12-21 06:46:05 +00:00
RASr <= (S==1 & Ref==0) | // Refresh
(S==4 & RAMSEL & nWE) | // Read: Early RAS
(S==5 & RAMSEL & ~nWE); // Write: Late RAS
2019-09-01 02:55:04 +00:00
2019-12-21 06:46:05 +00:00
// Multiplex DRAM address in at end of S4 through S6.
ASel = (RAMSEL & nWE & S==4) | // Read: mux address early
(RAMSEL & ~nWE & S==5); // Write: mux address late
2019-09-01 02:55:04 +00:00
2019-12-21 06:46:05 +00:00
// Read: long, early CAS, gated later by RAMSEL
CASr <= (RAMSEL & ~nWE & (S==5 | S==6 | S==7));
2019-09-01 02:55:04 +00:00
end
2020-01-26 20:13:37 +00:00
always @(negedge C7M) begin
if (S==0 | S==1) nRWE <= 1;
if (S==3) nRWE <= nWE;
2019-12-21 06:46:05 +00:00
RASf <= (S==4 & RAMSEL & nWE) | // Read: Early RAS
(S==5 & RAMSEL & ~nWE); // Write: Late RAS
2019-12-21 06:46:05 +00:00
CAS0f <= (S==1 & Ref==0) | // Refresh
(S==6 & RAMSEL & ~Addr[22] & ~nWE); // Write: Late CAS
CAS1f <= (S==1 & Ref==0) | // Refresh
(S==6 & RAMSEL & Addr[22] & ~nWE); // Write: Late CAS
2019-09-01 02:55:04 +00:00
end
endmodule