OpenCores
URL https://opencores.org/ocsvn/zx_ula/zx_ula/trunk

Subversion Repositories zx_ula

[/] [zx_ula/] [branches/] [xilinx/] [spectrum_48k_for_digilent_spartan3_starter_kit_with_ps2_keyboard/] [ula.v] - Blame information for rev 10

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 10 mcleod_ide
`timescale 1ns / 1ps
2
//////////////////////////////////////////////////////////////////////////////////
3
// Company:        Dept. Architecture and Computing Technology. University of Seville
4
// Engineer:       Miguel Angel Rodriguez Jodar. rodriguj@atc.us.es
5
// 
6
// Create Date:    19:13:39 4-Apr-2012 
7
// Design Name:    ZX Spectrum
8
// Module Name:    ula 
9
// Project Name: 
10
// Target Devices: 
11
// Tool versions: 
12
// Description: 
13
//
14
// Dependencies: 
15
//
16
// Revision: 
17
// Revision 1.00 - File Created
18
// Additional Comments: GPL License policies apply to the contents of this file.
19
//
20
//////////////////////////////////////////////////////////////////////////////////
21
 
22
`define cyclestart(a,b) ((a)==(b))
23
`define cycleend(a,b) ((a)==(b+1))
24
 
25
module ula(
26
    input clk14,                        // 14MHz master clock
27
         // CPU interfacing
28
    input [15:0] a,              // Address bus from CPU (not all lines are used)
29
    input [7:0] din,             // Input data bus from CPU
30
         output [7:0] dout,      // Output data bus to CPU
31
    input mreq_n,                       // MREQ from CPU
32
    input iorq_n,                       // IORQ from CPU
33
    input rd_n,                 // RD from CPU
34
    input wr_n,                 // WR from CPU
35
         input rfsh_n,                  // RFSH from CPU
36
         output clkcpu,         // CLK to CPU
37
         output msk_int_n,      // Vertical retrace interrupt, to CPU
38
         // VRAM interfacing
39
    output [13:0] va,     // Address bus to VRAM (16K)
40
         input [7:0] vramdout,// Data from VRAM to ULA/CPU
41
         output [7:0] vramdin,// Data from CPU to VRAM
42
    output vramoe,               // 
43
    output vramcs,               // Control signals for VRAM
44
    output vramwe,               //
45
         // ULA I/O
46
    input ear,                             //
47
    output mic,                    // I/O ports
48
    output spk,            //
49
         output [7:0] kbrows,   // Keyboard rows
50
    input [4:0] kbcolumns,       //  Keyboard columns
51
         // Video output
52
    output r,                           //
53
    output g,                           // RGB TTL signal
54
    output b,                           // with separate bright
55
    output i,                           // and composite sync
56
    output csync                        //               
57
    );
58
 
59
        reg [2:0] BorderColor = 3'b100;
60
 
61
        // Pixel clock
62
        reg clk7 = 0;
63
        always @(posedge clk14)
64
                clk7 <= !clk7;
65
 
66
        // Horizontal counter
67
        reg [8:0] hc = 0;
68
        always @(posedge clk7) begin
69
                if (hc==447)
70
                        hc <= 0;
71
                else
72
                        hc <= hc + 1;
73
        end
74
 
75
        // Vertical counter
76
        reg [8:0] vc = 0;
77
        always @(posedge clk7) begin
78
                if (hc==447) begin
79
                        if (vc == 311)
80
                                vc <= 0;
81
                        else
82
                                vc <= vc + 1;
83
                end
84
        end
85
 
86
        // HBlank generation
87
        reg HBlank_n = 1;
88
        always @(negedge clk7) begin
89
                if (`cyclestart(hc,320))
90
                        HBlank_n <= 0;
91
                else if (`cycleend(hc,415))
92
                        HBlank_n <= 1;
93
        end
94
 
95
        // HSync generation (6C ULA version)
96
        reg HSync_n = 1;
97
        always @(negedge clk7) begin
98
                if (`cyclestart(hc,344))
99
                        HSync_n <= 0;
100
                else if (`cycleend(hc,375))
101
                        HSync_n <= 1;
102
        end
103
 
104
        // VBlank generation
105
        reg VBlank_n = 1;
106
        always @(negedge clk7) begin
107
                if (`cyclestart(vc,248))
108
                        VBlank_n <= 0;
109
                else if (`cycleend(vc,255))
110
                        VBlank_n <= 1;
111
        end
112
 
113
        // VSync generation (PAL)
114
        reg VSync_n = 1;
115
        always @(negedge clk7) begin
116
                if (`cyclestart(vc,248))
117
                        VSync_n <= 0;
118
                else if (`cycleend(vc,251))
119
                        VSync_n <= 1;
120
        end
121
 
122
        // INT generation
123
        reg INT_n = 1;
124
        assign msk_int_n = INT_n;
125
        always @(negedge clk7) begin
126
                if (`cyclestart(vc,248) && `cyclestart(hc,0))
127
                        INT_n <= 0;
128
                else if (`cyclestart(vc,248) && `cycleend(hc,31))
129
                        INT_n <= 1;
130
        end
131
 
132
        // Border control signal (=0 when we're not displaying paper/ink pixels)
133
        reg Border_n = 1;
134
        always @(negedge clk7) begin
135
                if ( (vc[7] & vc[6]) | vc[8] | hc[8])
136
                        Border_n <= 0;
137
                else
138
                        Border_n <= 1;
139
        end
140
 
141
        // VidEN generation (delaying Border 8 clocks)
142
        reg VidEN_n = 1;
143
        always @(negedge clk7) begin
144
                if (hc[3])
145
                        VidEN_n <= !Border_n;
146
        end
147
 
148
        // DataLatch generation (posedge to capture data from memory)
149
        reg DataLatch_n = 1;
150
        always @(negedge clk7) begin
151
                if (hc[0] & !hc[1] & Border_n & hc[3])
152
                        DataLatch_n <= 0;
153
                else
154
                        DataLatch_n <= 1;
155
        end
156
 
157
        // AttrLatch generation (posedge to capture data from memory)
158
        reg AttrLatch_n = 1;
159
        always @(negedge clk7) begin
160
                if (hc[0] & hc[1] & Border_n & hc[3])
161
                        AttrLatch_n <= 0;
162
                else
163
                        AttrLatch_n <= 1;
164
        end
165
 
166
        // SLoad generation (negedge to load shift register)
167
        reg SLoad = 0;
168
        always @(negedge clk7) begin
169
                if (!hc[0] & !hc[1] & hc[2] & !VidEN_n)
170
                        SLoad <= 1;
171
                else
172
                        SLoad <= 0;
173
        end
174
 
175
        // AOLatch generation (negedge to update attr output latch)
176
        reg AOLatch_n = 1;
177
        always @(negedge clk7) begin
178
                if (hc[0] & !hc[1] & hc[2])
179
                        AOLatch_n <= 0;
180
                else
181
                        AOLatch_n <= 1;
182
        end
183
 
184
        // First buffer for bitmap
185
        reg [7:0] BitmapReg = 0;
186
        always @(negedge DataLatch_n) begin
187
                BitmapReg <= vramdout;
188
        end
189
 
190
        // Shift register (second bitmap register)
191
        reg [7:0] SRegister = 0;
192
        always @(negedge clk7) begin
193
                if (SLoad)
194
                        SRegister <= BitmapReg;
195
                else
196
                        SRegister <= {SRegister[6:0],1'b0};
197
        end
198
 
199
        // First buffer for attribute
200
        reg [7:0] AttrReg = 0;
201
        always @(negedge AttrLatch_n) begin
202
                AttrReg <= vramdout;
203
        end
204
 
205
        // Second buffer for attribute
206
        reg [7:0] AttrOut = 0;
207
        always @(negedge AOLatch_n) begin
208
                if (!VidEN_n)
209
                        AttrOut <= AttrReg;
210
                else
211
                        AttrOut <= {2'b00,BorderColor,BorderColor};
212
        end
213
 
214
        // Flash counter and pixel generation
215
        reg [4:0] FlashCnt = 0;
216
        always @(negedge VSync_n) begin
217
                FlashCnt <= FlashCnt + 1;
218
        end
219
        wire Pixel = SRegister[7] ^ (AttrOut[7] & FlashCnt[4]);
220
 
221
        // RGB generation
222
        reg rI,rG,rR,rB;
223
        assign r = rR;
224
        assign g = rG;
225
        assign b = rB;
226
        assign i = rI;
227
        always @(*) begin
228
                if (HBlank_n && VBlank_n)
229
                        {rI,rG,rR,rB} = (Pixel)? {AttrOut[6],AttrOut[2:0]} : {AttrOut[6],AttrOut[5:3]};
230
                else
231
                        {rI,rG,rR,rB} = 4'b0000;
232
        end
233
 
234
        //CSync generation
235
        assign csync = HSync_n & VSync_n;
236
 
237
        // VRAM address and control line generation
238
        reg [13:0] rVA = 0;
239
        reg rVCS = 0;
240
        reg rVOE = 0;
241
        reg rVWE = 0;
242
        assign va = rVA;
243
        assign vramcs = rVCS;
244
        assign vramoe = rVOE;
245
        assign vramwe = rVWE;
246
        // Latches to hold delayed versions of V and H counters
247
        reg [8:0] v = 0;
248
        reg [8:0] c = 0;
249
        // Address and control line multiplexor ULA/CPU
250
        always @(negedge clk7) begin
251
                if (Border_n && (hc[3:0]==4'b0111 || hc[3:0]==4'b1011)) begin     // cycles 7 and 11: load V and C from VC and HC
252
                        c <= hc;
253
                        v <= vc;
254
                end
255
        end
256
        // Address and control line multiplexor ULA/CPU
257
        always @(*) begin
258
                if (Border_n && (hc[3:0]==4'b1000 || hc[3:0]==4'b1001 || hc[3:0]==4'b1100 || hc[3:0]==4'b1101)) begin       // cycles 8 and 12: present display address to VRAM 
259
                        rVA = {1'b0,v[7:6],v[2:0],v[5:3],c[7:3]};                                                // (cycles 9 and 13 load display byte)
260
                        rVCS = 1;
261
                        rVOE = !hc[0];
262
                        rVWE = 0;
263
                end
264
                else if (Border_n && (hc[3:0]==4'b1010 || hc[3:0]==4'b1011 || hc[3:0]==4'b1110 || hc[3:0]==4'b1111)) begin  // cycles 10 and 14: present attribute address to VRAM
265
                        rVA = {4'b0110,v[7:3],c[7:3]};                                                                          // (cycles 11 and 15 load attr byte)
266
                        rVCS = 1;
267
                        rVOE = !hc[0];
268
                        rVWE = 0;
269
                end
270
                else if (Border_n && hc[3:0]==4'b0000) begin
271
                        rVA = a[13:0];
272
                        rVCS = 0;
273
                        rVOE = 0;
274
                        rVWE = 0;
275
                end
276
                else begin      // when VRAM is not in use by ULA, give it to CPU
277
                        rVA = a[13:0];
278
                        rVCS = !a[15] & a[14] & !mreq_n;
279
                        rVOE = !rd_n;
280
                        rVWE = !wr_n;
281
                end
282
        end
283
 
284
        // CPU contention
285
        reg CPUClk = 0;
286
        assign clkcpu = CPUClk;
287
        reg ioreqtw3 = 0;
288
        reg mreqt23 = 0;
289
        wire ioreq_n = a[0] | iorq_n;
290
        wire Nor1 = (~(a[14] | ~ioreq_n)) |
291
                    (~(~a[15] | ~ioreq_n)) |
292
                                        (~(hc[2] | hc[3])) |
293
                                        (~Border_n | ~ioreqtw3 | ~CPUClk | ~mreqt23);
294
        wire Nor2 = (~(hc[2] | hc[3])) |
295
                    ~Border_n |
296
                                        ~CPUClk |
297
                                        ioreq_n |
298
                                        ~ioreqtw3;
299
        wire CLKContention = ~Nor1 | ~Nor2;
300
 
301
        always @(posedge clk7) begin    // change clk7 by clk14 for 7MHz CPU clock operation
302
                if (CPUClk && !CLKContention)   // if there's no contention, the clock can go low
303
                        CPUClk <= 0;
304
                else
305
                        CPUClk <= 1;
306
        end
307
        always @(posedge CPUClk) begin
308
                ioreqtw3 <= ioreq_n;
309
                mreqt23 <= mreq_n;
310
        end
311
 
312
        // ULA-CPU interface
313
        assign dout = (!a[15] & a[14] & !mreq_n)? vramdout : // CPU reads VRAM through ULA as in the +3, not directly
314
                      (!iorq_n & !a[0])?          {1'b1,ear,1'b1,kbcolumns} :    // CPU reads keyboard and EAR state
315
                                          (Border_n)?                  AttrReg :  // to emulate
316
                                                                      8'hFF;     // port FF
317
        assign vramdin = din;           // The CPU doesn't need to share the memory input data bus with the ULA
318
        assign kbrows = {a[11]? 1'bz : 1'b0,    // high impedance or 0, as if diodes were been placed in between
319
                                                  a[10]? 1'bz : 1'b0,   // if the keyboard matrix is to be implemented within the FPGA, then
320
                                                  a[9]?  1'bz : 1'b0,   // there's no need to do this.
321
                                                  a[12]? 1'bz : 1'b0,
322
                                                  a[13]? 1'bz : 1'b0,
323
                                                  a[8]?  1'bz : 1'b0,
324
                                                  a[14]? 1'bz : 1'b0,
325
                                                  a[15]? 1'bz : 1'b0 };
326
        reg rMic = 0;
327
        reg rSpk = 0;
328
        assign mic = rMic;
329
        assign spk = rSpk;
330
        always @(negedge clk7) begin
331
                if (!iorq_n & !a[0] & !wr_n)
332
                        {rSpk,rMic,BorderColor} <= din[5:0];
333
        end
334
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.