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

Subversion Repositories next186mp3

[/] [next186mp3/] [trunk/] [HW/] [ddr_186.v] - Blame information for rev 3

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ndumitrach
//////////////////////////////////////////////////////////////////////////////////
2
//
3
// This file is part of the Next186 Soc PC project
4
// http://opencores.org/project,next186
5
//
6
// Filename: ddr_186.v
7
// Description: Part of the Next186 SoC PC project, main system, RAM interface
8
// Version 2.0
9
// Creation date: Apr2014
10
//
11
// Author: Nicolae Dumitrache 
12
// e-mail: ndumitrache@opencores.org
13
//
14
/////////////////////////////////////////////////////////////////////////////////
15
// 
16
// Copyright (C) 2012 Nicolae Dumitrache
17
// 
18
// This source file may be used and distributed without 
19
// restriction provided that this copyright statement is not 
20
// removed from the file and that any derivative work contains 
21
// the original copyright notice and the associated disclaimer.
22
// 
23
// This source file is free software; you can redistribute it 
24
// and/or modify it under the terms of the GNU Lesser General 
25
// Public License as published by the Free Software Foundation;
26
// either version 2.1 of the License, or (at your option) any 
27
// later version. 
28
// 
29
// This source is distributed in the hope that it will be 
30
// useful, but WITHOUT ANY WARRANTY; without even the implied 
31
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
32
// PURPOSE. See the GNU Lesser General Public License for more 
33
// details. 
34
// 
35
// You should have received a copy of the GNU Lesser General 
36
// Public License along with this source; if not, download it 
37
// from http://www.opencores.org/lgpl.shtml 
38
// 
39
///////////////////////////////////////////////////////////////////////////////////
40
// Additional Comments: 
41
//
42
// 25Apr2012 - added SD card SPI support
43
// 15May2012 - added PIT 8253 (sound + timer INT8)
44
// 24May2012 - added PIC 8259  
45
// 28May2012 - RS232 boot loader does not depend on CPU speed anymore (uses timer0)
46
//      01Feb2013 - ADD 8042 PS2 Keyboard & Mouse controller
47
// 27Feb2013 - ADD RTC
48
// 04Apr2013 - ADD NMI, port 3bc for 8 leds
49
//
50
// Feb2014 - ported for SDRAM, added USB host serial communication
51
//                 - added video modes 0dh, 12h
52
//                 - support for ModeX
53
//////////////////////////////////////////////////////////////////////////////////
54
 
55
/* ----------------- implemented ports -------------------
56
0001 - bit0=write RS232, bit1=write USB host out, bit2=USB host reset
57
          - bit0=auto cache flush, on WORD write only
58
 
59
0002 - 32 bit CPU data port R/W, lo first
60
0003 - 32 bit CPU command port W
61
                16'b00000cvvvvvvvvvv = set r/w pointer - 256 32bit integers, 1024 instructions. c=1 for code write, 0 for data read/write
62
                16'b100wwwvvvvvvvvvv = run ip - 1024 instructions, 3 bit data window offs
63
 
64
0021, 00a1 - interrupt controller data ports. R/W interrupt mask, 1disabled/0enabled (bit0=timer, bit1=keyboard, bit3=RTC, bit4=mouse)
65
 
66
0040-0043 - PIT 8253 ports
67
 
68
0x60, 0x64 - 8042 keyboard/mouse data and cfg
69
 
70
0061 - bits1:0 speaker on/off (write only)
71
 
72
0070 - RTC (16bit write only counter value). RTC is incremented with 1Mhz and at set value sends INT70h, then restart from 0
73
                 When set, it restarts from 0. If the set value is 0, it will send INT70h only once, if it was not already 0
74
 
75
080h-08fh - memory map: bit9:0=64 Kbytes DDRAM segment index (up to 1024 segs = 64MB), mapped over
76
                                                                PORT[3:0] 80186 addressable segment
77
 
78
0200h-020fh - joystick port - returns 0ffh
79
 
80
0378 - sound port: 8bit=Covox & DSS compatible, 16bit = stereo L+R - fifo sampled at 44100Hz
81
                 bit4 of port 03DA is 1 when the sound queue is full. If it is 0, the queue may accept up to 1152 stereo samples (L + R), so 2304 16bit writes.
82
 
83
0379 - parallel port control: bit6 = 1 when DSS queue is full
84
 
85
03C0 - VGA mode
86
                index 10h:
87
                        bit0 = graphic(1)/text(0)
88
                        bit3 = text mode flash enabled(1)
89
                        bit4 = half mode (EGA)
90
                        bit6 = 320x200(1)/640x480(0)
91
                index 13h: bit[3:0] = hrz pan
92
 
93
03C4, 03C5 (Sequencer registers) - idx2[3:0] = write plane, idx4[3]=0 for planar (rw)
94
 
95
03C6 - DAC mask (rw)
96
03C7 - DAC read index (rw)
97
03C8 - DAC write index (rw)
98
03C9 - DAC color (rw)
99
03CB - font: write WORD = set index (8 bit), r/w BYTE = r/w font data
100
 
101
03CE, 03CF (Graphics registers) (rw)
102
        0: setres <= din[3:0];
103
        1: enable_setres <= din[3:0];
104
        2: color_compare <= din[3:0];
105
        3: logop <= din[4:3];
106
        4: rplane <= din[1:0];
107
        5: rwmode <= {din[3], din[1:0]};
108
        7: color_dont_care <= din[3:0];
109
        8: bitmask <= din[7:0]; (1=CPU, 0=latch)
110
 
111
03DA - read VGA status, bit0=1 on vblank or hblank, bit1=RS232in, bit2=USB host serial in, bit3=1 on vblank, bit4=sound queue full, bit5=DSP32 halt, bit7=1 always, bit15:8=SD SPI byte read
112
                 write bit7=SD SPI MOSI bit, SPI CLK 0->1 (BYTE write only), bit8 = SD card chip select (WORD write only)
113
                 also reset the 3C0 port index flag
114
 
115
03B4, 03D4 - VGA CRT write index:
116
                                                                                06h: bit 7=1 for 200lines, 0 for 240 lines
117
                                                                                0Ah(bit 5 only): hide cursor
118
                                                                                0Ch: HI screen offset
119
                                                                                0Dh: LO screen offset
120
                                                                                0Eh: HI cursor pos
121
                                                                                0Fh: LO cursor pos
122
                                                                                13h: scan line offset
123
03B5, 03D5 - VGA CRT read/write data
124
*/
125
 
126
 
127
`timescale 1ns / 1ps
128
 
129
module system
130
        (
131
                 output sdr_CLK_out,
132
                 output [3:0]sdr_n_CS_WE_RAS_CAS,
133
                 output [1:0]sdr_BA,
134
                 output [12:0]sdr_ADDR,
135
                 inout [15:0]sdr_DATA,
136
                 output [1:0]sdr_DQM,
137
                 input  CLK_32MHZ,
138
                 output reg [5:0]VGA_R,
139
                 output reg [5:0]VGA_G,
140
                 output reg [5:0]VGA_B,
141
                 output wire VGA_HSYNC,
142
                 output wire VGA_VSYNC,
143
                 input BTN_RESET,       // Reset
144
                 input BTN_NMI,         // NMI
145
                 output [7:0]LED,        // HALT
146
                 input RS232_DCE_RXD,
147
                 output reg RS232_DCE_TXD,
148
                 input RS232_HOST_RXD,
149
                 output reg RS232_HOST_TXD,
150
                 output reg RS232_HOST_RST,
151
 
152
                 output reg SD_n_CS = 1,
153
                 output wire SD_DI,
154
                 output reg SD_CK = 0,
155
                 input SD_DO,
156
 
157
                 output reg AUD_L,
158
                 output reg AUD_R,
159
                 inout PS2_CLK1,
160
                 inout PS2_CLK2,
161
                 inout PS2_DATA1,
162
                 inout PS2_DATA2
163
    );
164
 
165
        wire [15:0]cntrl0_user_input_data;//i
166
        wire [1:0]sys_cmd_ack;
167
        wire sys_rd_data_valid;
168
        wire sys_wr_data_valid;
169
        wire [15:0]sys_DOUT;     // sdr data out
170
        wire [12:0]waddr;
171
        wire [31:0] DOUT;
172
        wire [15:0]CPU_DOUT;
173
        wire [15:0]PORT_ADDR;
174
        wire [31:0] DRAM_dout;
175
        wire [20:0] ADDR;
176
        wire IORQ;
177
        wire WR;
178
        wire INTA;
179
        wire WORD;
180
        wire [3:0] RAM_WMASK;
181
        wire Empty;     // fifo empty
182
        wire hblnk;
183
        wire vblnk;
184
        wire [9:0]hcount;
185
        wire [9:0]vcount;
186
        reg [3:0]vga_hrzpan = 0;
187
        wire [3:0]vga_hrzpan_req;
188
        wire [9:0]hcount_pan = hcount + vga_hrzpan - 17;
189
        wire displ_on = !(hblnk | vblnk | Empty);
190
        wire [17:0]DAC_COLOR;
191
        wire Full;
192
        wire AlmostFull;
193
        wire AlmostEmpty;
194
        wire clk_25;
195
        wire clk_cpu;
196
        wire clk_sdr;
197
        wire CPU_CE;    // CPU clock enable
198
        wire CE;
199
        wire CE_186;
200
        wire ddr_rd;
201
        wire ddr_wr;
202
        wire TIMER_OE = PORT_ADDR[15:2] == 14'b00000000010000;  //   40h..43h
203
        wire VGA_DAC_OE = PORT_ADDR[15:4] == 12'h03c && PORT_ADDR[3:0] <= 4'h9; // 3c0h..3c9h    
204
        wire LED_PORT = PORT_ADDR[15:0] == 16'h03bc;
205
        wire SPEAKER_PORT = PORT_ADDR[15:0] == 16'h0061;
206
        wire MEMORY_MAP = PORT_ADDR[15:4] == 12'h008;
207
        wire VGA_FONT_OE = PORT_ADDR[15:0] == 16'h03cb;
208
        wire RS232_OE = PORT_ADDR[15:0] == 16'h0001;
209
        wire INPUT_STATUS_OE = PORT_ADDR[15:0] == 16'h03da;
210
        wire VGA_CRT_OE = (PORT_ADDR[15:1] == 15'b000000111011010) || (PORT_ADDR[15:1] == 15'b000000111101010); // 3b4h, 3b5h, 3d4h, 3d5h
211
        wire RTC_SELECT = PORT_ADDR[15:0] == 16'h0070;
212
        wire VGA_SC = PORT_ADDR[15:1] == (16'h03c4 >> 1); // 3c4h, 3c5h
213
        wire VGA_GC = PORT_ADDR[15:1] == (16'h03ce >> 1); // 3ceh, 3cfh
214
        wire PIC_OE = PORT_ADDR[15:8] == 8'h00 && PORT_ADDR[6:0] == 7'b0100001;  // 21h, a1h
215
        wire KB_OE = PORT_ADDR[15:4] == 12'h006 && {PORT_ADDR[3], PORT_ADDR[1:0]} == 3'b000; // 60h, 64h
216
        wire JOYSTICK = PORT_ADDR[15:4] == 12'h020; // 0x200-0x20f
217
        wire PARALLEL_PORT = PORT_ADDR[15:0] == 16'h0378;
218
        wire PARALLEL_PORT_CTL = PORT_ADDR[15:0] == 16'h0379;
219
        wire CPU32_PORT = PORT_ADDR[15:1] == (16'h0002 >> 1); // port 1 for data and 3 for instructions
220
        wire [7:0]VGA_DAC_DATA;
221
        wire [7:0]VGA_CRT_DATA;
222
        wire [7:0]VGA_SC_DATA;
223
        wire [7:0]VGA_GC_DATA;
224
        wire [15:0]PORT_IN;
225
        wire [7:0]TIMER_DOUT;
226
        wire [7:0]KB_DOUT;
227
        wire [7:0]PIC_DOUT;
228
        wire HALT;
229
        wire CLK44100x256;
230
        wire sq_full; // sound queue full
231
        wire dss_full;
232
        wire AUDIO_L;
233
        wire AUDIO_R;
234
        wire [15:0]cpu32_data;
235
        wire cpu32_halt;
236
 
237
        reg [1:0]cntrl0_user_command_register = 0;
238
        reg [16:0]vga_ddr_row_col = 0; // video buffer offset (multiple of 4)
239
        reg s_prog_full;
240
        reg s_prog_empty;
241
        reg [1:0]s_ddr_rd = 0;
242
        reg [1:0]s_ddr_wr = 0;
243
        reg crw = 0;     // 1=cache read window
244
        reg s_RS232_DCE_RXD;
245
        reg s_RS232_HOST_RXD;
246
        reg [4:0]rstcount = 0;
247
        reg [18:0]s_displ_on = 0; // clk_25 delayed displ_on
248
        reg [2:0]vga400 = 0;              // 1 for 400 lines, 0 for 480 lines
249
        reg [2:0]vgatext = 0;             // 1 for text mode
250
        reg [2:0]v240 = 0;
251
        reg [2:0]planar = 0;
252
        reg [2:0]half = 0;
253
        wire vgaflash;
254
        reg flashbit = 0;
255
        reg [5:0]flashcount = 0;
256
        wire [11:0]charcount = {vcount[8:4], 4'b0000} + {vcount[8:4], 6'b000000} + hcount_pan[9:3];
257
        wire [31:0]fifo_dout32;
258
        wire [15:0]fifo_dout = (vgatext[1] ? hcount_pan[3] : vga400[1] ? hcount_pan[2] : hcount_pan[1]) ? fifo_dout32[31:16] : fifo_dout32[15:0];
259
 
260
        reg [8:0]vga_ddr_row_count = 0;
261
        reg [2:0]max_read;
262
        reg [4:0]col_counter;
263
        wire vga_end_frame = vga_ddr_row_count == (v240[0] ? 479 : 399);
264
        reg [3:0]vga_repln_count = 0; // repeat line counter
265
        wire [3:0]vga_repln = vgatext[0] ? 15 : (vga400[0] | half[0]) ? 1 : 0;
266
        reg [7:0]vga_lnbytecount = 0; // line byte count (multiple of 4)
267
        wire [4:0]vga_lnend = (vgatext[0] | half[0]) ? 5 : (vga400[0] | planar[0]) ? 10 : 20; // multiple of 32 (SDRAM resolution = 32)
268
        reg [11:0]vga_font_counter = 0;
269
        reg [7:0]vga_attr;
270
        reg [4:0]RTCDIV25 = 0;
271
        reg [1:0]RTCSYNC = 0;
272
        reg [15:0]RTC = 0;
273
        reg [15:0]RTCSET = 0;
274
        wire RTCEND = RTC == RTCSET;
275
        wire RTCDIVEND = RTCDIV25 == 24;
276
        reg [12:0]cache_hi_addr;
277
        wire [8:0]memmap;
278
        wire [8:0]memmap_mux;
279
        wire [7:0]font_dout;
280
        wire [7:0]VGA_FONT_DATA;
281
        wire vgatextreq;
282
        wire vga400req;
283
        wire planarreq;
284
        wire v240req;
285
        wire halfreq;
286
        wire oncursor;
287
        wire [11:0]cursorpos;
288
        wire [15:0]scraddr;
289
        reg flash_on;
290
        reg speaker_on = 0;
291
        reg [9:0]rNMI = 0;
292
        wire [2:0]shift = half[1] ? ~hcount_pan[3:1] : ~hcount_pan[2:0];
293
        wire [2:0]pxindex = -hcount_pan[2:0];
294
        wire [3:0]VGA_MUX = vgatext[1] ? (font_dout[pxindex] ^ flash_on) ? vga_attr[3:0] : {vga_attr[7] & ~vgaflash, vga_attr[6:4]} :
295
                                                                         {fifo_dout32[{2'b11, shift}], fifo_dout32[{2'b10, shift}], fifo_dout32[{2'b01, shift}], fifo_dout32[{2'b00, shift}]};
296
        reg s_vga_endline;
297
        reg s_vga_endscanline = 0;
298
        reg s_vga_endframe;
299
        reg [20:0]sdraddr;
300
        wire [3:0]vga_wplane;
301
        wire [1:0]vga_rplane;
302
        wire [7:0]vga_bitmask;   // write 1=CPU, 0=VGA latch
303
        wire [2:0]vga_rwmode;
304
        wire [3:0]vga_setres;
305
        wire [3:0]vga_enable_setres;
306
        wire [1:0]vga_logop;
307
        wire [3:0]vga_color_compare;
308
        wire [3:0]vga_color_dont_care;
309
        wire [7:0]vga_offset;
310
        reg [2:0]auto_flush = 3'b000;
311
//      wire slowport = !(INPUT_STATUS_OE || CPU32_PORT);
312
//      wire WAITIO_ADDR = IORQ && !WR && CE_186 && slowport;
313
//      reg rWAITIO_ADDR = 1'b0;
314
//      wire WAITIO = WAITIO_ADDR && !rWAITIO_ADDR;
315
//      reg [7:0]slowportdata;
316
 
317
        assign LED = {1'b0, !cpu32_halt, AUDIO_L, AUDIO_R, planarreq, |sys_cmd_ack, ~SD_n_CS, HALT};
318
 
319
// SD interface
320
        reg [7:0]SDI;
321
        assign SD_DI = CPU_DOUT[7];
322
 
323
        assign PORT_IN[15:8] =
324
                ({8{MEMORY_MAP}} & {7'b0000000, memmap[8]}) |
325
                ({8{INPUT_STATUS_OE}} & SDI) |
326
                ({8{CPU32_PORT}} & cpu32_data[15:8]);
327
 
328
        assign PORT_IN[7:0] = //INPUT_STATUS_OE ? {2'b1x, cpu32_halt, sq_full, vblnk, s_RS232_HOST_RXD, s_RS232_DCE_RXD, hblnk | vblnk} : CPU32_PORT ? cpu32_data[7:0] : slowportdata;
329
                                                         ({8{VGA_DAC_OE}} & VGA_DAC_DATA) |
330
                                                         ({8{VGA_FONT_OE}}& VGA_FONT_DATA) |
331
                                                         ({8{KB_OE}} & KB_DOUT) |
332
                                                         ({8{INPUT_STATUS_OE}} & {2'b1x, cpu32_halt, sq_full, vblnk, s_RS232_HOST_RXD, s_RS232_DCE_RXD, hblnk | vblnk}) |
333
                                                         ({8{VGA_CRT_OE}} & VGA_CRT_DATA) |
334
                                                         ({8{MEMORY_MAP}} & {memmap[7:0]}) |
335
                                                         ({8{TIMER_OE}} & TIMER_DOUT) |
336
                                                         ({8{PIC_OE}} & PIC_DOUT) |
337
                                                         ({8{VGA_SC}} & VGA_SC_DATA) |
338
                                                         ({8{VGA_GC}} & VGA_GC_DATA) |
339
                                                         ({8{JOYSTICK}})|
340
                                                         ({8{PARALLEL_PORT_CTL}} & {1'bx, dss_full, 6'bxxxxxx}) |
341
                                                         ({8{CPU32_PORT}} & cpu32_data[7:0]);
342
 
343
        dcm dcm_system
344
        (
345
                .CLK_IN1(CLK_32MHZ),
346
                .CLK_OUT1(clk_25),
347
                .CLK_OUT2(clk_sdr),
348
                .CLK_OUT3(sdr_CLK_out),
349
                .CLK_OUT4(clk_cpu),
350
                .CLK_OUT5(CLK44100x256)
351
    );
352
 
353
 
354
        SDRAM_16bit SDR
355
        (
356
                .sys_CLK(clk_sdr),                              // clock
357
                .sys_CMD(cntrl0_user_command_register),                                 // 00=nop, 01 = write 256 bytes, 10=read 32 bytes, 11=read 256 bytes
358
                .sys_ADDR(sdraddr),     // word address
359
                .sys_DIN(cntrl0_user_input_data),               // data input
360
                .sys_DOUT(sys_DOUT),                                    // data output
361
                .sys_rd_data_valid(sys_rd_data_valid),  // data valid read
362
                .sys_wr_data_valid(sys_wr_data_valid),  // data valid write
363
                .sys_cmd_ack(sys_cmd_ack),                      // command acknowledged
364
 
365
                .sdr_n_CS_WE_RAS_CAS(sdr_n_CS_WE_RAS_CAS),                      // SDRAM #CS, #WE, #RAS, #CAS
366
                .sdr_BA(sdr_BA),                                        // SDRAM bank address
367
                .sdr_ADDR(sdr_ADDR),                            // SDRAM address
368
                .sdr_DATA(sdr_DATA),                            // SDRAM data
369
                .sdr_DQM(sdr_DQM)                                       // SDRAM DQM
370
        );
371
 
372
        fifo vga_fifo
373
        (
374
          .wr_clk(clk_sdr), // input wr_clk
375
          .rd_clk(clk_25), // input rd_clk
376
          .din(sys_DOUT), // input [15 : 0] din
377
          .wr_en(!crw && sys_rd_data_valid && !col_counter[4]), // input wr_en
378
          .rd_en(s_displ_on[~vga_hrzpan] && ((vgatext[1] | half[1]) ? &hcount_pan[3:0] : (vga400[1] | planar[1]) ? &hcount_pan[2:0] : &hcount_pan[1:0])), // input rd_en
379
          .dout({fifo_dout32[15:0], fifo_dout32[31:16]}), // output [31 : 0] dout
380
          .full(Full), // output full
381
          .empty(Empty), // output empty
382
          .prog_full(AlmostFull), // output prog_full
383
          .prog_empty(AlmostEmpty) // output prog_empty
384
        );
385
 
386
        VGA_SG VGA
387
        (
388
                .tc_hsblnk(10'd639),
389
                .tc_hssync(10'd655 + 10'd17),     // +17 for hrz panning
390
                .tc_hesync(10'd751 + 10'd17),     // +17 for hrz panning
391
                .tc_heblnk(10'd799),
392
                .hcount(hcount),
393
                .hsync(VGA_HSYNC),
394
                .hblnk(hblnk),
395
                .tc_vsblnk(v240[2] ? 10'd479 : 10'd399),
396
                .tc_vssync(v240[2] ? 10'd489 : 10'd411),
397
                .tc_vesync(v240[2] ? 10'd491 : 10'd413),
398
                .tc_veblnk(v240[2] ? 10'd520 : 10'd446),
399
                .vcount(vcount),
400
                .vsync(VGA_VSYNC),
401
                .vblnk(vblnk),
402
                .clk(clk_25),
403
                .ce(!Empty)
404
        );
405
 
406
        VGA_DAC dac
407
        (
408
                 .CE(VGA_DAC_OE && IORQ && CPU_CE),
409
                 .WR(WR),
410
                 .addr(PORT_ADDR[3:0]),
411
                 .din(CPU_DOUT[7:0]),
412
                 .dout(VGA_DAC_DATA),
413
                 .CLK(clk_cpu),
414
                 .VGA_CLK(clk_25),
415
                 .vga_addr((vgatext[1] | (~vga400[1] & planar[1])) ? {4'b0000, VGA_MUX} : (vga400[1] ? hcount_pan[1] : hcount_pan[0]) ? fifo_dout[15:8] : fifo_dout[7:0]),
416
                 .color(DAC_COLOR),
417
                 .vgatext(vgatextreq),
418
                 .vga400(vga400req),
419
                 .half(halfreq),
420
                 .vgaflash(vgaflash),
421
                 .setindex(INPUT_STATUS_OE && IORQ && CPU_CE),
422
                 .hrzpan(vga_hrzpan_req)
423
    );
424
 
425
         VGA_CRT crt
426
         (
427
                .CE(IORQ && CPU_CE && VGA_CRT_OE),
428
                .WR(WR),
429
                .WORD(WORD),
430
                .din(CPU_DOUT),
431
                .addr(PORT_ADDR[0]),
432
                .dout(VGA_CRT_DATA),
433
                .CLK(clk_cpu),
434
                .oncursor(oncursor),
435
                .cursorpos(cursorpos),
436
                .scraddr(scraddr),
437
                .v240(v240req),
438
                .offset(vga_offset)
439
        );
440
 
441
        VGA_SC sc
442
        (
443
                .CE(IORQ && CPU_CE && VGA_SC),  // 3c4, 3c5
444
                .WR(WR),
445
                .WORD(WORD),
446
                .din(CPU_DOUT),
447
                .dout(VGA_SC_DATA),
448
                .addr(PORT_ADDR[0]),
449
                .CLK(clk_cpu),
450
                .planarreq(planarreq),
451
                .wplane(vga_wplane)
452
    );
453
 
454
        VGA_GC gc
455
        (
456
                .CE(IORQ && CPU_CE && VGA_GC),
457
                .WR(WR),
458
                .WORD(WORD),
459
                .din(CPU_DOUT),
460
                .addr(PORT_ADDR[0]),
461
                .CLK(clk_cpu),
462
                .rplane(vga_rplane),
463
                .bitmask(vga_bitmask),
464
                .rwmode(vga_rwmode),
465
                .setres(vga_setres),
466
                .enable_setres(vga_enable_setres),
467
                .logop(vga_logop),
468
                .color_compare(vga_color_compare),
469
                .color_dont_care(vga_color_dont_care),
470
                .dout(VGA_GC_DATA)
471
        );
472
 
473
        sr_font VGA_FONT
474
        (
475
                .clka(clk_25), // input clka
476
                .wea(1'b0), // input [0 : 0] wea
477
                .addra({fifo_dout[7:0], vcount[3:0]}), // input [11 : 0] addra
478
                .dina(8'h00), // input [7 : 0] dina
479
                .douta(font_dout), // output [7 : 0] douta
480
                .clkb(clk_cpu), // input clkb
481
                .web(WR & IORQ & VGA_FONT_OE & ~WORD & CPU_CE), // input [0 : 0] web
482
                .addrb(vga_font_counter), // input [11 : 0] addrb
483
                .dinb(CPU_DOUT[7:0]), // input [7 : 0] dinb
484
                .doutb(VGA_FONT_DATA) // output [7 : 0] doutb
485
        );
486
 
487
        cache_controller cache_ctl
488
        (
489
                 .addr(ADDR),
490
                 .dout(DRAM_dout),
491
                 .din(DOUT),
492
                 .clk(clk_cpu),
493
                 .mreq(MREQ),
494
                 .wmask(RAM_WMASK),
495
                 .ce(CE),
496
                 .ddr_din(sys_DOUT),
497
                 .ddr_dout(cntrl0_user_input_data),
498
                 .ddr_clk(clk_sdr),
499
                 .ddr_rd(ddr_rd),
500
                 .ddr_wr(ddr_wr),
501
                 .waddr(waddr),
502
                 .cache_write_data(crw && sys_rd_data_valid), // read DDR, write to cache
503
                 .cache_read_data(crw && sys_wr_data_valid),
504
                 .flush(auto_flush == 3'b101)
505
        );
506
 
507
        wire I_KB;
508
        wire I_MOUSE;
509
        wire KB_RST;
510
        KB_Mouse_8042 KB_Mouse
511
        (
512
                 .CS(IORQ && CPU_CE && KB_OE), // 60h, 64h
513
                 .WR(WR),
514
                 .cmd(PORT_ADDR[2]), // 64h
515
                 .din(CPU_DOUT[7:0]),
516
                 .dout(KB_DOUT),
517
                 .clk(clk_cpu),
518
                 .I_KB(I_KB),
519
                 .I_MOUSE(I_MOUSE),
520
                 .CPU_RST(KB_RST),
521
                 .PS2_CLK1(PS2_CLK1),
522
                 .PS2_CLK2(PS2_CLK2),
523
                 .PS2_DATA1(PS2_DATA1),
524
                 .PS2_DATA2(PS2_DATA2)
525
        );
526
 
527
        wire [7:0]PIC_IVECT;
528
        wire INT;
529
        wire timer_int;
530
        PIC_8259 PIC
531
        (
532
                 .CS(PIC_OE && IORQ && CPU_CE), // 21h, a1h
533
                 .WR(WR),
534
                 .din(CPU_DOUT[7:0]),
535
                 .dout(PIC_DOUT),
536
                 .ivect(PIC_IVECT),
537
                 .clk(clk_cpu),
538
                 .INT(INT),
539
                 .IACK(INTA & CPU_CE),
540
                 .I({I_MOUSE, RTCEND, I_KB, timer_int})
541
    );
542
 
543
        unit186 CPUUnit
544
        (
545
                 .INPORT(INTA ? {8'h00, PIC_IVECT} : PORT_IN),
546
                 .DIN(DRAM_dout),
547
                 .CPU_DOUT(CPU_DOUT),
548
                 .PORT_ADDR(PORT_ADDR),
549
                 .DOUT(DOUT),
550
                 .ADDR(ADDR),
551
                 .WMASK(RAM_WMASK),
552
                 .CLK(clk_cpu),
553
                 .CE(CE/* & !WAITIO*/),
554
                 .CPU_CE(CPU_CE),
555
                 .CE_186(CE_186),
556
                 .INTR(INT),
557
                 .NMI(rNMI[9]),
558
                 .RST(BTN_RESET || !rstcount[4]),
559
                 .INTA(INTA),
560
                 .LOCK(LOCK),
561
                 .HALT(HALT),
562
                 .MREQ(MREQ),
563
                 .IORQ(IORQ),
564
                 .WR(WR),
565
                 .WORD(WORD),
566
 
567
                 .PLANAR(planarreq),
568
                 .VGA_WPLANE(vga_wplane),
569
                 .VGA_RPLANE(vga_rplane),
570
                 .VGA_BITMASK(vga_bitmask),
571
                 .VGA_RWMODE(vga_rwmode),
572
                 .VGA_SETRES(vga_setres),
573
                 .VGA_ENABLE_SETRES(vga_enable_setres),
574
                 .VGA_LOGOP(vga_logop),
575
                 .VGA_COLOR_COMPARE(vga_color_compare),
576
                 .VGA_COLOR_DONT_CARE(vga_color_dont_care)
577
        );
578
 
579
        seg_map seg_mapper
580
        (
581
                 .CLK(clk_cpu),
582
                 .cpuaddr(PORT_ADDR[3:0]),
583
                 .cpurdata(memmap),
584
                 .cpuwdata(CPU_DOUT[8:0]),
585
                 .memaddr(cache_hi_addr[12:8]),
586
                 .memdata(memmap_mux),
587
                 .WE(MEMORY_MAP & WR & WORD & IORQ & CPU_CE)
588
    );
589
 
590
         wire timer_spk;
591
         timer_8253 timer
592
         (
593
                 .CS(TIMER_OE && IORQ && CPU_CE),
594
                 .WR(WR),
595
                 .addr(PORT_ADDR[1:0]),
596
                 .din(CPU_DOUT[7:0]),
597
                 .dout(TIMER_DOUT),
598
                 .CLK_25(clk_25),
599
                 .clk(clk_cpu),
600
                 .out0(timer_int),
601
                 .out2(timer_spk)
602
    );
603
 
604
         soundwave sound_gen
605
         (
606
                .CLK(clk_cpu),
607
                .CLK44100x256(CLK44100x256),
608
                .data(CPU_DOUT),
609
                .we(IORQ & CPU_CE & WR & PARALLEL_PORT),
610
                .word(WORD),
611
                .full(sq_full), // when not full, write max 2x1152 16bit samples
612
                .dss_full(dss_full),
613
                .AUDIO_L(AUDIO_L),
614
                .AUDIO_R(AUDIO_R)
615
        );
616
 
617
        DSP32 DSP32_inst
618
        (
619
                .clk(clk_cpu),
620
                .cmd(PORT_ADDR[0]), // port 2=data, port 3=cmd (word only)
621
                .ce(IORQ & CPU_CE & CPU32_PORT & WORD),
622
                .wr(WR),
623
                .din(CPU_DOUT),
624
                .dout(cpu32_data),
625
                .halt(cpu32_halt)
626
        );
627
 
628
        reg nop;
629
        always @ (posedge clk_sdr) begin
630
                s_prog_full <= AlmostFull;
631
                s_prog_empty <= AlmostEmpty;
632
                s_ddr_rd <= {s_ddr_rd[0], ddr_rd};
633
                s_ddr_wr <= {s_ddr_wr[0], ddr_wr};
634
                s_vga_endline <= vga_repln_count == vga_repln;
635
                s_vga_endframe <= vga_end_frame;
636
                cache_hi_addr <= s_ddr_wr[0] ? waddr : ADDR[20:8];
637
                nop <= sys_cmd_ack == 2'b00;
638
                sdraddr <= s_prog_empty || !(s_ddr_wr[1] || s_ddr_rd[1]) ? {4'b0001, vga_ddr_row_col + vga_lnbytecount} : {memmap_mux[6:0], cache_hi_addr[7:0], 6'b000000};
639
                max_read <= &sdraddr[6:3] ? ~sdraddr[2:0] : 3'b111;      // SDRAM row size = 256 words
640
 
641
                if(s_prog_empty) cntrl0_user_command_register <= 2'b10;                 // read 32 bytes VGA
642
                else if(s_ddr_wr[1]) cntrl0_user_command_register <= 2'b01;             // write 256 bytes cache
643
                else if(s_ddr_rd[1]) cntrl0_user_command_register <= 2'b11;             // read 256 bytes cache
644
                else if(~s_prog_full) cntrl0_user_command_register <= 2'b10;    // read 32 bytes VGA
645
                else cntrl0_user_command_register <= 2'b00;
646
 
647
                if(!crw && sys_rd_data_valid) col_counter <= col_counter - 1;
648
                if(nop) case(sys_cmd_ack)
649
                        2'b10: begin
650
                                crw <= 1'b0;    // VGA read
651
                                col_counter <= {1'b0, max_read, 1'b1};
652
                                vga_lnbytecount <= vga_lnbytecount + max_read + 1;
653
                        end
654
                        2'b01, 2'b11: crw <= 1'b1;      // cache read/write                     
655
                endcase
656
 
657
                if(s_vga_endscanline) begin
658
                        col_counter[3:1] <= col_counter[3:1] - vga_lnbytecount[2:0];
659
                        vga_lnbytecount <= 0;
660
                        s_vga_endscanline <= 1'b0;
661
 
662
                        if(s_vga_endframe) vga_ddr_row_col <= {{1'b0, scraddr[15:13]} + (vgatext[0] ? 4'b0111 : 4'b0100), scraddr[12:0]};
663
                        else if(s_vga_endline) vga_ddr_row_col <= vga_ddr_row_col + (vgatext[0] ? 40 : {vga_offset, 1'b0});
664
 
665
                        if(s_vga_endline) vga_repln_count <= 0;
666
                        else vga_repln_count <= vga_repln_count + 1;
667
                        if(s_vga_endframe) begin
668
                                vga400[0] <= vga400req;
669
                                vgatext[0] <= vgatextreq;
670
                                v240[0] <= v240req;
671
                                planar[0] <= planarreq;
672
                                half[0] <= halfreq;
673
                                vga_ddr_row_count <= 0;
674
                        end else vga_ddr_row_count <= vga_ddr_row_count + 1;
675
                end else s_vga_endscanline <= (vga_lnbytecount[7:3] == vga_lnend);
676
        end
677
 
678
        always @ (posedge clk_cpu) begin
679
                s_RS232_DCE_RXD <= RS232_DCE_RXD;
680
                s_RS232_HOST_RXD <= RS232_HOST_RXD;
681
                if(IORQ & CPU_CE) begin
682
                        if(WR & RS232_OE) begin
683
                                {RS232_HOST_RST, RS232_HOST_TXD, RS232_DCE_TXD} <= CPU_DOUT[2:0];
684
                                if(WORD) auto_flush[2] <= CPU_DOUT[0];
685
                        end
686
                        if(VGA_FONT_OE) vga_font_counter <= WR && WORD ? {CPU_DOUT[7:0], 4'b0000} : vga_font_counter + 1;
687
                        if(WR & SPEAKER_PORT) speaker_on <= &CPU_DOUT[1:0];
688
//                      if(WR & LED_PORT) LED <= CPU_DOUT[7:0];
689
                end
690
// SD
691
                if(CPU_CE) begin
692
                        SD_CK <= IORQ & INPUT_STATUS_OE & WR & ~WORD;
693
                        if(IORQ & INPUT_STATUS_OE & WR) begin
694
                                if(WORD) SD_n_CS <= ~CPU_DOUT[8]; // SD chip select
695
                                else SDI <= {SDI[6:0], SD_DO};
696
                        end
697
                end
698
 
699
                if(KB_RST) rstcount <= 0;
700
                else if(CPU_CE && ~rstcount[4]) rstcount <= rstcount + 1;
701
 
702
// RTC          
703
                RTCSYNC <= {RTCSYNC[0], RTCDIVEND};
704
                if(IORQ && CPU_CE && WR && WORD && RTC_SELECT) begin
705
                        RTC <= 0;
706
                        RTCSET <= CPU_DOUT;
707
                end else if(RTCSYNC == 2'b01) begin
708
                        if(RTCEND) RTC <= 0;
709
                        else RTC <= RTC + 1;
710
                end
711
 
712
                auto_flush[1:0] <= {auto_flush[0], vblnk};
713
 
714
/*              rWAITIO_ADDR <= WAITIO_ADDR;
715
 
716
                slowportdata <= ({8{VGA_DAC_OE}} & VGA_DAC_DATA) |
717
                                                         ({8{VGA_FONT_OE}}& VGA_FONT_DATA) |
718
                                                         ({8{KB_OE}} & KB_DOUT) |
719
                                                         ({8{VGA_CRT_OE}} & VGA_CRT_DATA) |
720
                                                         ({8{MEMORY_MAP}} & {memmap[7:0]}) |
721
                                                         ({8{TIMER_OE}} & TIMER_DOUT) |
722
                                                         ({8{PIC_OE}} & PIC_DOUT) |
723
                                                         ({8{VGA_SC}} & VGA_SC_DATA) |
724
                                                         ({8{VGA_GC}} & VGA_GC_DATA) |
725
                                                         ({8{JOYSTICK}})|
726
                                                         ({8{PARALLEL_PORT_CTL}} & {1'bx, dss_full, 6'bxxxxxx});
727
*/
728
        end
729
 
730
        always @ (posedge clk_25) begin
731
                s_displ_on <= {s_displ_on[17:0], displ_on};
732
                vga_attr <= fifo_dout[15:8];
733
                flash_on <= (vgaflash & fifo_dout[15] & flashcount[5]) | (~oncursor && flashcount[4] && (charcount == cursorpos) && ((vcount[3:0] == 13) || vcount[3:0] == 14));
734
 
735
                if(!vblnk) begin
736
                        flashbit <= 1;
737
                        vga400[2] <= vga400[1];
738
                        vgatext[2] <= vgatext[1];
739
                        v240[2] <= v240[1];
740
                        planar[2] <= planar[1];
741
                        half[2] <= half[1];
742
                end else if(flashbit) begin
743
                        flashcount <= flashcount + 1;
744
                        flashbit <= 0;
745
                        vga400[1] <= vga400[0];
746
                        vgatext[1] <= vgatext[0];
747
                        v240[1] <= v240[0];
748
                        planar[1] <= planar[0];
749
                        half[1] <= half[0];
750
                end
751
 
752
                if(RTCDIVEND) RTCDIV25 <= 0;     // real time clock
753
                else RTCDIV25 <= RTCDIV25 + 1;
754
 
755
                if(!BTN_NMI) rNMI <= 0;          // NMI
756
                else if(!rNMI[9] && RTCDIVEND) rNMI <= rNMI + 1;        // 1Mhz increment
757
 
758
                {VGA_B, VGA_G, VGA_R} <= DAC_COLOR & {18{s_displ_on[17+vgatext[1]]}};
759
                if(VGA_VSYNC) vga_hrzpan <= half[0] ? {vga_hrzpan_req[2:0], 1'b0} : {1'b0, vga_hrzpan_req[2:0]};
760
        end
761
 
762
        reg [4:0]mix;
763
        always @(posedge CLK44100x256) begin
764
                mix <= mix + 1'b1;
765
                AUD_L <= &mix & speaker_on ? timer_spk : AUDIO_L;
766
                AUD_R <= &mix & speaker_on ? timer_spk : AUDIO_R;
767
        end
768
 
769
endmodule
770
 

powered by: WebSVN 2.1.0

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