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

Subversion Repositories rf6809

[/] [rf6809/] [trunk/] [rtl/] [noc/] [video/] [rfSpriteController_x12.sv] - Blame information for rev 19

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 19 robfinch
`timescale 1ns / 1ps
2
// ============================================================================
3
//        __
4
//   \\__/ o\    (C) 2005-2022  Robert Finch, Waterloo
5
//    \  __ /    All rights reserved.
6
//     \/_//     robfinch@finitron.ca
7
//       ||
8
//
9
//      rfSpriteController_x12.v
10
//              sprite / hardware cursor controller, 12-bit slave bus
11
//
12
// BSD 3-Clause License
13
// Redistribution and use in source and binary forms, with or without
14
// modification, are permitted provided that the following conditions are met:
15
//
16
// 1. Redistributions of source code must retain the above copyright notice, this
17
//    list of conditions and the following disclaimer.
18
//
19
// 2. Redistributions in binary form must reproduce the above copyright notice,
20
//    this list of conditions and the following disclaimer in the documentation
21
//    and/or other materials provided with the distribution.
22
//
23
// 3. Neither the name of the copyright holder nor the names of its
24
//    contributors may be used to endorse or promote products derived from
25
//    this software without specific prior written permission.
26
//
27
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
31
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
//
38
//
39
//      Sprite Controller
40
//
41
//      FEATURES
42
//      - parameterized number of sprites 1,2,4,6,8,14 or 32
43
//      - sprite image cache buffers
44
//              - each image cache is capable of holding multiple
45
//                sprite images
46
//              - an embedded DMA controller is used for sprite reload
47
//      - programmable image offset within cache
48
//      - programmable sprite width,height, and pixel size
49
//              - sprite width and height may vary from 1 to 64 as long
50
//                as the product doesn't exceed 4096.
51
//          - pixels may be programmed to be 1,2,3 or 4 video clocks
52
//            both height and width are programmable
53
//      - programmable sprite position
54
//      - programmable 8, 16 or 32 bits for color
55
//              eg 32k color + 1 bit alpha blending indicator (1,5,5,5)
56
//      - fixed display and DMA priority
57
//          sprite 0 highest, sprite 31 lowest
58
//      - graphics plane control
59
//
60
//              This core requires an external timing generator to
61
//      provide horizontal and vertical sync signals, but
62
//      otherwise can be used as a display controller on it's
63
//      own. However, normally this core would be embedded
64
//      within another core such as a VGA controller. Sprite
65
//      positions are referenced to the rising edge of the
66
//      vertical and horizontal sync pulses.
67
//              The core includes an embedded dual port RAM to hold the
68
//      sprite images. The image RAM is updated using a built in DMA
69
//      controller. The DMA controller uses 32 bit accesses to fill
70
//      the sprite buffers. The circuit features an automatic bus
71
//  transaction timeout; if the system bus hasn't responded
72
//  within 20 clock cycles, the DMA controller moves onto the
73
//  next address.
74
//              The controller uses a ram underlay to cache the values
75
//      of the registers. This is a lot cheaper resource wise than
76
//      using a 32 to 1 multiplexor (well at least for an FPGA).
77
//
78
//      All registers are 32 bits wide
79
//
80
//      These registers repeat in incrementing block of four registers
81
//      and pertain to each sprite
82
//      00:     - position register
83
//              HPOS    [11: 0] horizontal position (hctr value)
84
//          VPOS        [27:16] vertical position (vctr value)
85
//
86
//      04:     SZ      - size register
87
//                      bits
88
//                      [ 7: 0] width of sprite in pixels - 1
89
//                      [15: 8] height of sprite in pixels -1
90
//                      [19:16] size of horizontal pixels - 1 in clock cycles
91
//                      [23:20] size of vertical pixels in scan-lines - 1
92
//                              * the product of width * height cannot exceed 2048 !
93
//                              if it does, the display will begin repeating
94
//                      [27:24] output plane
95
//                      [31:30] color depth 01=RGB332,10=RGB555+A,11=RGB888+A
96
//
97
//      08: ADR [31:12] 20 bits sprite image address bits
98
//                      This registers contain the high order address bits of the
99
//          location of the sprite image in system memory.
100
//                      The DMA controller will assign the low order 12 bits
101
//                      during DMA.
102
//                  [11:0] image offset bits [11:0]
103
//                      offset of the sprite image within the sprite image cache
104
//                      typically zero
105
//
106
//      0C: TC  [23:0]  transparent color
107
//                      This register identifies which color of the sprite
108
//                      is transparent
109
//
110
//
111
//
112
//      0C-1FC: registers reserved for up to thirty-one other sprites
113
//
114
//      200:            DMA burst reg sprite 0
115
//                              [8:0]  burst start
116
//                              [24:16] burst end
117
//      ...
118
//      27C:            DMA burst reg sprite 31
119
//
120
//      Global status and control
121
//      3C0: EN [31:0] sprite enable register
122
//  3C4: IE     [31:0] sprite interrupt enable / status
123
//      3C8: SCOL       [31:0] sprite-sprite collision register
124
//      3CC: BCOL       [31:0] sprite-background collision register
125
//      3D0: DT         [31:0] sprite DMA trigger on
126
//      3D4: DT         [31:0] sprite DMA trigger off
127
//      3D8: VDT        [31:0] sprite vertical sync DMA trigger
128
//      3EC: BC     [23:0] background color
129
//  3FC: ADDR   [31:0] sprite DMA address bits [63:32]
130
//
131
//=============================================================================
132
 
133
module rfSpriteController_x12(
134
// Bus Slave interface
135
//------------------------------
136
// Slave signals
137
input rst_i,                    // reset
138
input s_clk_i,                  // clock
139
input         s_cs_i,
140
input         s_cyc_i,  // cycle valid
141
input         s_stb_i,  // data transfer
142
output        s_ack_o,  // transfer acknowledge
143
input         s_we_i,   // write
144
input  [ 9:0] s_adr_i,  // address
145
input  [11:0] s_dat_i,  // data input
146
output reg [11:0] s_dat_o,      // data output
147
//------------------------------
148
// Bus Master Signals
149
input m_clk_i,                          // clock
150
output [1:0]  m_bte_o,
151
output [2:0]  m_cti_o,
152
output reg    m_cyc_o,  // cycle is valid
153
output        m_stb_o,  // strobe output
154
input         m_ack_i,  // input data is ready
155
input         m_err_i,
156
output        m_we_o,
157
output [3:0] m_sel_o,
158
output reg [35:0] m_adr_o,      // DMA address
159
input  [47:0] m_dat_i,  // data input
160
output [47:0] m_dat_o,
161
output [4:0] m_spriteno_o,
162
//--------------------------
163
input dot_clk_i,                // video dot clock
164
input hsync_i,                  // horizontal sync pulse
165
input vsync_i,                  // vertical sync pulse
166
input blank_i,                  // blanking signal
167
//input [3:0] rgbPlane_i,               // 0 = background, higher numbers closer to front
168
input [31:0] zrgb_i,                    // input pixel stream
169
//output reg [3:0] rgbPlane_o,
170
output [31:0] zrgb_o,   // output pixel stream
171
output irq,                                     // interrupt request
172
input test
173
);
174
 
175
reg m_soc_o;
176
wire vclk = dot_clk_i;
177
wire hSync = hsync_i;
178
wire vSync = vsync_i;
179
wire [31:0] zrgbIn = zrgb_i;
180
reg [31:0] zrgbOut;
181
assign zrgb_o = zrgbOut;
182
 
183
//--------------------------------------------------------------------
184
// Core Parameters
185
//--------------------------------------------------------------------
186
parameter pnSpr = 32;           // number of sprites
187
parameter phBits = 12;          // number of bits in horizontal timing counter
188
parameter pvBits = 12;          // number of bits in vertical timing counter
189
localparam pnSprm = pnSpr-1;
190
 
191
 
192
//--------------------------------------------------------------------
193
// Variable Declarations
194
//--------------------------------------------------------------------
195
 
196
reg [9:0] adr_i;
197
reg [11:0] dat_i;
198
reg we_i;
199
 
200
wire [4:0] sprN = adr_i[8:4];
201
 
202
reg [phBits-1:0] hctr;          // horizontal reference counter (counts dots since hSync)
203
reg [pvBits-1:0] vctr;          // vertical reference counter (counts scanlines since vSync)
204
reg sprSprIRQ;
205
reg sprBkIRQ;
206
 
207
reg [23:0] out;                 // sprite output
208
reg outact;                             // sprite output is active
209
reg [3:0] outplane;
210
reg [pnSprm:0] bkCollision;             // sprite-background collision
211
reg [23:0] bgTc;                // background transparent color
212
reg [23:0] bkColor;             // background color
213
 
214
 
215
reg [pnSprm:0] sprWe;   // block ram write enable for image cache update
216
reg [pnSprm:0] sprRe;   // block ram read enable for image cache update
217
 
218
// Global control registers
219
reg [31:0] sprEn;       // enable sprite
220
reg [pnSprm:0] sprCollision;        // sprite-sprite collision
221
reg sprSprIe;                   // sprite-sprite interrupt enable
222
reg sprBkIe;            // sprite-background interrupt enable
223
reg sprSprIRQPending;   // sprite-sprite collision interrupt pending
224
reg sprBkIRQPending;    // sprite-background collision interrupt pending
225
reg sprSprIRQPending1;  // sprite-sprite collision interrupt pending
226
reg sprBkIRQPending1;   // sprite-background collision interrupt pending
227
reg sprSprIRQ1;                 // vclk domain regs
228
reg sprBkIRQ1;
229
 
230
// Sprite control registers
231
reg [31:0] sprSprCollision;
232
reg [pnSprm:0] sprSprCollision1;
233
reg [31:0] sprBkCollision;
234
reg [pnSprm:0] sprBkCollision1;
235
reg [23:0] sprTc [pnSprm:0];            // sprite transparent color code
236
// How big the pixels are:
237
// 1 to 16 video clocks
238
reg [3:0] hSprRes [pnSprm:0];           // sprite horizontal resolution
239
reg [3:0] vSprRes [pnSprm:0];           // sprite vertical resolution
240
reg [7:0] sprWidth [pnSprm:0];          // number of pixels in X direction
241
reg [7:0] sprHeight [pnSprm:0];         // number of vertical pixels
242
reg [3:0] sprPlane [pnSprm:0];          // output plane sprite is in
243
reg [1:0] sprColorDepth [pnSprm:0];
244
reg [1:0] colorBits;
245
// Sprite DMA control
246
reg [8:0] sprBurstStart [pnSprm:0];
247
reg [8:0] sprBurstEnd   [pnSprm:0];
248
reg [31:0] vSyncT;                                                              // DMA on vSync
249
 
250
// display and timing signals
251
reg [31:0] hSprReset;   // horizontal reset
252
reg [31:0] vSprReset;   // vertical reset
253
reg [31:0] hSprDe;              // sprite horizontal display enable
254
reg [31:0] vSprDe;              // sprite vertical display enable
255
reg [31:0] sprDe;                       // display enable
256
reg [phBits-1:0] hSprPos [pnSprm:0];    // sprite horizontal position
257
reg [pvBits-1:0] vSprPos [pnSprm:0];    // sprite vertical position
258
reg [7:0] hSprCnt [pnSprm:0];   // sprite horizontal display counter
259
reg [7:0] vSprCnt [pnSprm:0];   // vertical display counter
260
reg [11:0] sprImageOffs [pnSprm:0];     // offset within sprite memory
261
reg [12:0] sprAddr [pnSprm:0];  // index into sprite memory (pixel number)
262
reg [9:0] sprAddr1 [pnSprm:0];  // index into sprite memory
263
reg [9:0] sprAddr2 [pnSprm:0];  // index into sprite memory
264
reg [9:0] sprAddr3 [pnSprm:0];  // index into sprite memory
265
reg [9:0] sprAddr4 [pnSprm:0];  // index into sprite memory
266
reg [11:0] sprAddrB [pnSprm:0]; // backup address cache for rescan
267
wire [23:0] sprOut4 [pnSprm:0]; // sprite image data output
268
reg [23:0] sprOut [pnSprm:0];   // sprite image data output
269
reg [23:0] sprOut5 [pnSprm:0];  // sprite image data output
270
 
271
// DMA access
272
reg [31:12] sprSysAddr [pnSprm:0];      // system memory address of sprite image (low bits)
273
reg [4:0] dmaOwner;                     // which sprite has the DMA channel
274
reg [31:0] sprDt;               // DMA trigger register
275
reg dmaActive;                          // this flag indicates that a block DMA transfer is active
276
 
277
genvar g;
278
 
279
//--------------------------------------------------------------------
280
// DMA control / bus interfacing
281
//--------------------------------------------------------------------
282
reg cs_regs;
283
always_ff @(posedge s_clk_i)
284
        cs_regs <= s_cyc_i & s_stb_i & s_cs_i;
285
always_ff @(posedge s_clk_i)
286
        adr_i <= s_adr_i;
287
always_ff @(posedge s_clk_i)
288
        dat_i <= s_dat_i;
289
always_ff @(posedge s_clk_i)
290
        we_i <= s_we_i;
291
 
292
ack_gen #(
293
        .READ_STAGES(3),
294
        .WRITE_STAGES(1),
295
        .REGISTER_OUTPUT(1)
296
)
297
uag1 (
298
        .clk_i(s_clk_i),
299
        .ce_i(1'b1),
300
        .i(cs_regs),
301
        .we_i(cs_regs & we_i),
302
        .o(s_ack_o)
303
);
304
 
305
assign irq = sprSprIRQ|sprBkIRQ;
306
 
307
//--------------------------------------------------------------------
308
// DMA control / bus interfacing
309
//--------------------------------------------------------------------
310
 
311
reg [5:0] dmaStart;
312
reg [8:0] cob;  // count of burst cycles
313
 
314
assign m_bte_o = 2'b00;
315
assign m_cti_o = 3'b000;
316
assign m_stb_o = m_cyc_o;
317
assign m_we_o = 1'b0;
318
assign m_sel_o = {4{m_cyc_o}};
319
assign m_dat_o = 48'h0;
320
assign m_spriteno_o = dmaOwner;
321
 
322
reg [2:0] mstate;
323
parameter IDLE = 3'd0;
324
parameter ACTIVE = 3'd1;
325
parameter ACK = 3'd2;
326
parameter NACK = 3'd3;
327
 
328
wire pe_m_ack_i;
329
edge_det ued2 (.rst(rst_i), .clk(m_clk_i), .ce(1'b1), .i(m_ack_i), .pe(pe_m_ack_i), .ne(), .ee());
330
 
331
always_ff @(posedge m_clk_i)
332
if (rst_i)
333
        mstate <= IDLE;
334
else begin
335
        case(mstate)
336
        IDLE:
337
                if (|sprDt)
338
                        mstate <= ACTIVE;
339
        ACTIVE:
340
                mstate <= ACK;
341
        ACK:
342
                if (m_ack_i | m_err_i)
343
                        mstate <= NACK;
344
        NACK:
345
                if (~(m_ack_i|m_err_i))
346
                        mstate <= cob==sprBurstEnd[dmaOwner] ? IDLE : ACTIVE;
347
        default:
348
                mstate <= IDLE;
349
        endcase
350
end
351
 
352
integer n30;
353
always_ff @(posedge m_clk_i)
354
begin
355
        case(mstate)
356
        IDLE:
357
                begin
358
                        dmaOwner <= 5'd0;
359
                        for (n30 = pnSprm; n30 >= 0; n30 = n30 - 1)
360
                                if (sprDt[n30])
361
                                        dmaOwner <= n30;
362
                end
363
        default:        ;
364
        endcase
365
end
366
 
367
always_ff @(posedge m_clk_i)
368
if (rst_i)
369
        dmaStart <= 6'b0;
370
else begin
371
        dmaStart <= {dmaStart[4:0],1'b0};
372
        case(mstate)
373
        IDLE:
374
                if (|sprDt)
375
                        dmaStart <= 6'h3F;
376
        default:        ;
377
        endcase
378
end
379
 
380
integer n32;
381
always_ff @(posedge m_clk_i)
382
begin
383
        case(mstate)
384
        IDLE:
385
                for (n32 = pnSprm; n32 >= 0; n32 = n32 - 1)
386
                        if (sprDt[n32])
387
                                cob <= sprBurstStart[n32];
388
        ACTIVE:
389
                cob <= cob + 2'd1;
390
        default:        ;
391
        endcase
392
end
393
 
394
always_ff @(posedge m_clk_i)
395
if (rst_i)
396
        wb_m_nack();
397
else begin
398
        case(mstate)
399
        IDLE:
400
                wb_m_nack();
401
        ACTIVE:
402
                begin
403
                        m_cyc_o <= 1'b1;
404
                        m_adr_o <= {sprSysAddr[dmaOwner],cob[8:0],3'h0};
405
                end
406
        ACK:
407
                if (m_ack_i|m_err_i)
408
                        wb_m_nack();
409
        endcase
410
end
411
 
412
task wb_m_nack;
413
begin
414
        m_cyc_o <= 1'b0;
415
        m_adr_o <= 36'h0;
416
end
417
endtask
418
 
419
 
420
// generate a write enable strobe for the sprite image memory
421
integer n1;
422
always_ff @(posedge m_clk_i)
423
for (n1 = 0; n1 < pnSpr; n1 = n1 + 1)
424
        sprWe[n1] <= (dmaOwner==n1 && m_ack_i);
425
 
426
reg [8:0] m_adr_or;
427
reg [47:0] m_dat_ir;
428
always_ff @(posedge m_clk_i)
429
if (m_ack_i)
430
        m_adr_or <= m_adr_o[11:3];
431
always_ff @(posedge m_clk_i)
432
if (m_ack_i) begin
433
        if (test)
434
                m_dat_ir <= {4{1'b0,dmaOwner,10'b0}};
435
        else
436
                m_dat_ir <= m_dat_i;
437
end
438
 
439
//--------------------------------------------------------------------
440
//--------------------------------------------------------------------
441
 
442
reg [11:0] reg_shadow [0:1023];
443
reg [9:0] radr;
444
always_ff @(posedge s_clk_i)
445
begin
446
    if (cs_regs & we_i)  reg_shadow[adr_i[9:0]] <= dat_i;
447
end
448
always @(posedge s_clk_i)
449
    radr <= adr_i[9:0];
450
wire [11:0] reg_shadow_o = reg_shadow[radr];
451
 
452
// register/sprite memory output mux
453
always_ff @(posedge s_clk_i)
454
        if (cs_regs)
455
                case (adr_i[9:0])               // synopsys full_case parallel_case
456
                10'b1111000000: s_dat_o <= 12'h0;
457
                10'b1111000001: s_dat_o <= {4'h0,sprEn[31:24]};
458
                10'b1111000010: s_dat_o <= sprEn[23:12];
459
                10'b1111000011: s_dat_o <= sprEn[11: 0];
460
                10'b1111000100: s_dat_o <= {10'b0,sprBkIe,sprSprIe};
461
                10'b1111000101: s_dat_o <= {4'h0,sprBkIRQPending|sprSprIRQPending,5'b0,sprBkIRQPending,sprSprIRQPending};
462
                10'b1111001000: s_dat_o <= 12'h0;
463
                10'b1111001001: s_dat_o <= {4'h0,sprSprCollision[31:24]};
464
                10'b1111001010: s_dat_o <= sprSprCollision[23:12];
465
                10'b1111001011: s_dat_o <= sprSprCollision[11: 0];
466
                10'b1111001100: s_dat_o <= 12'h0;
467
                10'b1111001101: s_dat_o <= {4'h0,sprBkCollision[31:24]};
468
                10'b1111001110: s_dat_o <= sprBkCollision[23:12];
469
                10'b1111001111: s_dat_o <= sprBkCollision[11: 0];
470
                10'b1111010000: s_dat_o <= 12'h0;
471
                10'b1111010001: s_dat_o <= {4'h0,sprDt[31:24]};
472
                10'b1111010010: s_dat_o <= sprDt[23:12];
473
                10'b1111010011: s_dat_o <= sprDt[11: 0];
474
                default:        s_dat_o <= reg_shadow_o;
475
                endcase
476
        else
477
                s_dat_o <= 12'h0;
478
 
479
 
480
// vclk -> clk_i
481
always @(posedge s_clk_i)
482
begin
483
        sprSprIRQ <= sprSprIRQ1;
484
        sprBkIRQ <= sprBkIRQ1;
485
        sprSprIRQPending <= sprSprIRQPending1;
486
        sprBkIRQPending <= sprBkIRQPending1;
487
        sprSprCollision <= sprSprCollision1;
488
        sprBkCollision <= sprBkCollision1;
489
end
490
 
491
 
492
// register updates
493
// on the clk_i domain
494
reg vSync1;
495
integer n33;
496
always_ff @(posedge s_clk_i)
497
if (rst_i) begin
498
        vSyncT <= 32'hFFFFFFFF;
499
        sprEn <= 32'hFFFFFFFF;
500
        sprDt <= 0;
501
  for (n33 = 0; n33 < pnSpr; n33 = n33 + 1) begin
502
                sprSysAddr[n33] <= 24'b0000_0000_0000_0011_0000_0000 + n33;     //0030_0000
503
        end
504
        sprSprIe <= 0;
505
        sprBkIe  <= 0;
506
 
507
  // Set reasonable starting positions on the screen
508
  // so that the sprites might be visible for testing
509
  for (n33 = 0; n33 < pnSpr; n33 = n33 + 1) begin
510
    hSprPos[n33] <= 200 + (n33 & 7) * 70;
511
    vSprPos[n33] <= 100 + (n33 >> 3) * 100;
512
    sprTc[n33] <= 24'h396739;
513
                sprWidth[n33] <= 8'd56;  // 56x36 sprites
514
                sprHeight[n33] <= 8'd36;
515
                hSprRes[n33] <= 0;      // our standard display
516
                vSprRes[n33] <= 0;
517
                sprImageOffs[n33] <= 0;
518
                sprPlane[n33] <= 4'hF;//n[3:0];
519
                sprBurstStart[n33] <= 9'h000;
520
                sprBurstEnd[n33] <= 9'h1FF;
521
                sprColorDepth[n33] <= 2'b10;
522
        end
523
  hSprPos[0] <= 210;
524
  vSprPos[0] <= 72;
525
 
526
  bgTc <= 24'h08_08_08;
527
  bkColor <= 24'hFF_FF_60;
528
end
529
else begin
530
        vSync1 <= vSync;
531
        if (vSync & ~vSync1)
532
                sprDt <= sprDt | vSyncT;
533
 
534
        // clear DMA trigger bit once DMA is recognized
535
        if (dmaStart[5])
536
                sprDt[dmaOwner] <= 1'b0;
537
 
538
        if (cs_regs & we_i) begin
539
 
540
                casez (adr_i[9:0])
541
                10'b100?????00: sprBurstStart[adr_i[6:2]] <= dat_i[8:0];
542
                10'b100?????01: sprBurstEnd[adr_i[6:2]] <= dat_i[8:0];
543
                10'b1111000000: ;       // 3C0
544
                10'b1111000001: sprEn[31:24] <= dat_i[7:0];
545
                10'b1111000010: sprEn[23:12] <= dat_i;
546
                10'b1111000011: sprEn[11: 0] <= dat_i;
547
                10'b1111000100: // 3C4
548
                        begin
549
                                sprSprIe <= dat_i[0];
550
                                sprBkIe <= dat_i[1];
551
                        end
552
                // update DMA trigger
553
                // s_dat_i[7:0] indicates which triggers to set  (1=set,0=ignore)
554
                // s_dat_i[7:0] indicates which triggers to clear (1=clear,0=ignore)
555
                10'b1111010000: ;       // 3D0
556
                10'b1111010001: sprDt[31:24] <= sprDt[31:24] | dat_i[7:0];
557
                10'b1111010010: sprDt[23:12] <= sprDt[23:12] | dat_i;
558
                10'b1111010011: sprDt[11: 0] <= sprDt[11: 0] | dat_i;
559
                10'b1111010100: ;       // 3D4
560
                10'b1111010101: sprDt[31:24] <= sprDt[31:24] & ~dat_i[7:0];
561
                10'b1111010110: sprDt[23:12] <= sprDt[23:12] & ~dat_i;
562
                10'b1111010111: sprDt[11: 0] <= sprDt[11: 0] & ~dat_i;
563
                10'b1111011000: ;       // 3D8
564
                10'b1111011001: vSyncT[31:24] <= dat_i[7:0];
565
                10'b1111011010: vSyncT[23:12] <= dat_i;
566
                10'b1111011011: vSyncT[11: 0] <= dat_i;
567
                10'b1111101000: ;       // 3E8
568
                10'b1111101001: ;
569
                10'b1111101010: bgTc[23:12] <= dat_i;
570
                10'b1111101011: bgTc[11: 0] <= dat_i;
571
                10'b1111101100: ;       // 3EC
572
                10'b1111101101: ;
573
                10'b1111101110: bkColor[23:12] <= dat_i;
574
                10'b1111101111: bkColor[11: 0] <= dat_i;
575
                10'b0?????0000: hSprPos[sprN] <= dat_i[10: 0];
576
                10'b0?????0001: vSprPos[sprN] <= dat_i[10: 0];
577
                10'b0?????0100: sprWidth[sprN] <= dat_i[7:0];
578
                10'b0?????0101: sprHeight[sprN] <= dat_i[7:0];
579
                10'b0?????0110:
580
                                begin
581
                hSprRes[sprN] <= dat_i[3:0];
582
                vSprRes[sprN] <= dat_i[7:4];
583
                                end
584
                10'b0?????0111:
585
                                begin
586
                                        sprPlane[sprN] <= dat_i[3:0];
587
                                        sprColorDepth[sprN] <= dat_i[7:6];
588
                                end
589
                10'b0?????1000: ;// DMA address set on clk_i domain
590
                10'b0?????1001: sprSysAddr[sprN][31:24] <= dat_i[7:0];
591
                10'b0?????1010: sprSysAddr[sprN][23:12] <= dat_i;
592
                10'b0?????1011: sprImageOffs[sprN][10:0] <= dat_i[10:0];
593
                10'b0?????1100: sprTc[sprN][23:12] <= dat_i;
594
                10'b0?????1101: sprTc[sprN][11: 0] <= dat_i;
595
                default:        ;
596
                endcase
597
 
598
        end
599
end
600
 
601
//-------------------------------------------------------------
602
// Sprite Image Cache RAM
603
// This RAM is dual ported with an SoC side and a display
604
// controller side.
605
//-------------------------------------------------------------
606
 
607
integer n2;
608
always_ff @(posedge vclk)
609
for (n2 = 0; n2 < pnSpr; n2 = n2 + 1)
610
case(sprColorDepth[n2])
611
2'd1:   sprAddr1[n2] <= sprAddr[n2][11:2];
612
2'd2:   sprAddr1[n2] <= sprAddr[n2][10:1];
613
2'd3:   sprAddr1[n2] <= sprAddr[n2][ 9:0];
614
default:        ;
615
endcase
616
 
617
integer n4, n5, n27;
618
always_ff @(posedge vclk)
619
for (n4 = 0; n4 < pnSpr; n4 = n4 + 1)
620
        sprAddr2[n4] <= sprAddr1[n4];
621
always_ff @(posedge vclk)
622
for (n5 = 0; n5 < pnSpr; n5 = n5 + 1)
623
        sprAddr3[n5] <= sprAddr2[n5];
624
always_ff @(posedge vclk)
625
for (n27 = 0; n27 < pnSpr; n27 = n27 + 1)
626
        sprAddr4[n27] <= sprAddr3[n27];
627
 
628
// The pixels are displayed from most signicant to least signicant bits of the
629
// word. Display order is opposite to memory storage. So, the least significant
630
// address bits are flipped to get the correct display.
631
integer n3;
632
always_ff @(posedge vclk)
633
for (n3 = 0; n3 < pnSpr; n3 = n3 + 1)
634
case(sprColorDepth[n3])
635
2'd1:
636
        case(~sprAddr4[n3][1:0])
637
        2'd3:   sprOut5[n3] <= sprOut4[n3][23:18];
638
        2'd2:   sprOut5[n3] <= sprOut4[n3][17:12];
639
        2'd1:   sprOut5[n3] <= sprOut4[n3][11:6];
640
        2'd0:   sprOut5[n3] <= sprOut4[n3][5:0];
641
        endcase
642
2'd2:
643
        case(~sprAddr4[n3][0])
644
        1'd0:   sprOut5[n3] <= {sprOut4[n3][12],20'h0000,sprOut4[n3][10:0]};
645
        1'd1:   sprOut5[n3] <= {sprOut4[n3][23],20'h0000,sprOut4[n3][22:12]};
646
        endcase
647
2'd3:
648
        sprOut5[n3] <= sprOut4[n3];
649
default:        ;
650
endcase
651
 
652
generate
653
for (g = 0; g < pnSpr; g = g + 1) begin : sprRam
654
        SpriteRam_x12 sprRam0
655
        (
656
                .clka(m_clk_i),
657
                .addra(m_adr_or),
658
                .dina(m_dat_ir),
659
                .ena(sprWe[g]),
660
                .wea(sprWe[g]),
661
                // Core reg and output reg 3 clocks from read address
662
                .clkb(vclk),
663
                .addrb(sprAddr1[g]),
664
                .doutb(sprOut4[g]),
665
                .enb(1'b1)
666
        );
667
        end
668
endgenerate
669
 
670
//-------------------------------------------------------------
671
// Timing counters and addressing
672
// Sprites are like miniature bitmapped displays, they need
673
// all the same timing controls.
674
//-------------------------------------------------------------
675
 
676
// Create a timing reference using horizontal and vertical
677
// sync
678
wire hSyncEdge, vSyncEdge;
679
edge_det ed0(.rst(rst_i), .clk(vclk), .ce(1'b1), .i(hSync), .pe(hSyncEdge), .ne(), .ee() );
680
edge_det ed1(.rst(rst_i), .clk(vclk), .ce(1'b1), .i(vSync), .pe(vSyncEdge), .ne(), .ee() );
681
 
682
always_ff @(posedge vclk)
683
if (hSyncEdge) hctr <= {phBits{1'b0}};
684
else hctr <= hctr + 2'd1;
685
 
686
always_ff @(posedge vclk)
687
if (vSyncEdge) vctr <= {pvBits{1'b0}};
688
else if (hSyncEdge) vctr <= vctr + 2'd1;
689
 
690
// track sprite horizontal reset
691
integer n19;
692
always_ff @(posedge vclk)
693
for (n19 = 0; n19 < pnSpr; n19 = n19 + 1)
694
        hSprReset[n19] <= hctr==hSprPos[n19];
695
 
696
// track sprite vertical reset
697
integer n20;
698
always_ff @(posedge vclk)
699
for (n20 = 0; n20 < pnSpr; n20 = n20 + 1)
700
        vSprReset[n20] <= vctr==vSprPos[n20];
701
 
702
integer n21;
703
always_comb
704
for (n21 = 0; n21 < pnSpr; n21 = n21 + 1)
705
        sprDe[n21] <= hSprDe[n21] & vSprDe[n21];
706
 
707
 
708
// take care of sprite size scaling
709
// video clock division
710
reg [31:0] hSprNextPixel;
711
reg [31:0] vSprNextPixel;
712
reg [3:0] hSprPt [31:0];   // horizontal pixel toggle
713
reg [3:0] vSprPt [31:0];   // vertical pixel toggle
714
integer n17;
715
always_comb
716
for (n17 = 0; n17 < pnSpr; n17 = n17 + 1)
717
    hSprNextPixel[n17] = hSprPt[n17]==hSprRes[n17];
718
integer n18;
719
always_comb
720
for (n18 = 0; n18 < pnSpr; n18 = n18 + 1)
721
    vSprNextPixel[n18] = vSprPt[n18]==vSprRes[n18];
722
 
723
// horizontal pixel toggle counter
724
integer n6;
725
always_ff @(posedge vclk)
726
for (n6 = 0; n6 < pnSpr; n6 = n6 + 1)
727
        if (hSprReset[n6])
728
                hSprPt[n6] <= 4'd0;
729
  else if (hSprNextPixel[n6])
730
    hSprPt[n6] <= 4'd0;
731
  else
732
    hSprPt[n6] <= hSprPt[n6] + 2'd1;
733
 
734
// vertical pixel toggle counter
735
integer n7;
736
always_ff @(posedge vclk)
737
for (n7 = 0; n7 < pnSpr; n7 = n7 + 1)
738
  if (hSprReset[n7]) begin
739
        if (vSprReset[n7])
740
                vSprPt[n7] <= 4'd0;
741
    else if (vSprNextPixel[n7])
742
      vSprPt[n7] <= 4'd0;
743
    else
744
      vSprPt[n7] <= vSprPt[n7] + 2'd1;
745
  end
746
 
747
 
748
// clock sprite image address counters
749
integer n8;
750
always_ff @(posedge vclk)
751
for (n8 = 0; n8 < pnSpr; n8 = n8 + 1) begin
752
    // hReset and vReset - top left of sprite,
753
    // reset address to image offset
754
        if (hSprReset[n8] & vSprReset[n8]) begin
755
                sprAddr[n8]  <= sprImageOffs[n8];
756
                sprAddrB[n8] <= sprImageOffs[n8];
757
        end
758
        // hReset:
759
        //  If the next vertical pixel
760
        //      set backup address to current address
761
        //  else
762
        //      set current address to backup address
763
        //      in order to rescan the line
764
        else if (hSprReset[n8]) begin
765
                if (vSprNextPixel[n8])
766
                        sprAddrB[n8] <= sprAddr[n8];
767
                else
768
                        sprAddr[n8]  <= sprAddrB[n8];
769
        end
770
        // Not hReset or vReset - somewhere on the sprite scan line
771
        // just advance the address when the next pixel should be
772
        // fetched
773
        else if (hSprDe[n8] & hSprNextPixel[n8])
774
                sprAddr[n8] <= sprAddr[n8] + 2'd1;
775
end
776
 
777
 
778
// clock sprite column (X) counter
779
integer n9;
780
always_ff @(posedge vclk)
781
for (n9 = 0; n9 < pnSpr; n9 = n9 + 1)
782
        if (hSprReset[n9])
783
                hSprCnt[n9] <= 8'd1;
784
        else if (hSprNextPixel[n9])
785
                hSprCnt[n9] <= hSprCnt[n9] + 2'd1;
786
 
787
 
788
// clock sprite horizontal display enable
789
integer n10;
790
always_ff @(posedge vclk)
791
for (n10 = 0; n10 < pnSpr; n10 = n10 + 1) begin
792
        if (hSprReset[n10])
793
                hSprDe[n10] <= 1'b1;
794
        else if (hSprNextPixel[n10]) begin
795
                if (hSprCnt[n10] == sprWidth[n10])
796
                        hSprDe[n10] <= 1'b0;
797
        end
798
end
799
 
800
 
801
// clock the sprite row (Y) counter
802
integer n11;
803
always_ff @(posedge vclk)
804
for (n11 = 0; n11 < pnSpr; n11 = n11 + 1)
805
        if (hSprReset[n11]) begin
806
                if (vSprReset[n11])
807
                        vSprCnt[n11] <= 8'd1;
808
                else if (vSprNextPixel[n11])
809
                        vSprCnt[n11] <= vSprCnt[n11] + 2'd1;
810
        end
811
 
812
 
813
// clock sprite vertical display enable
814
integer n12;
815
always_ff @(posedge vclk)
816
for (n12 = 0; n12 < pnSpr; n12 = n12 + 1) begin
817
        if (hSprReset[n12]) begin
818
                if (vSprReset[n12])
819
                        vSprDe[n12] <= 1'b1;
820
                else if (vSprNextPixel[n12]) begin
821
                        if (vSprCnt[n12] == sprHeight[n12])
822
                                vSprDe[n12] <= 1'b0;
823
                end
824
        end
825
end
826
 
827
 
828
//-------------------------------------------------------------
829
// Output stage
830
//-------------------------------------------------------------
831
 
832
// function used for color blending
833
// given an alpha and a color component, determine the resulting color
834
// this blends towards black or white
835
// alpha is eight bits ranging between 0 and 1.999...
836
// 1 bit whole, 7 bits fraction
837
function [7:0] fnBlend;
838
input [7:0] alpha;
839
input [7:0] colorbits;
840
 
841
begin
842
        fnBlend = (({8'b0,colorbits} * alpha) >> 7);
843
end
844
endfunction
845
 
846
 
847
// pipeline delays for display enable
848
reg [31:0] sprDe1, sprDe2, sprDe3, sprDe4, sprDe5, sprDe6;
849
reg [31:0] sproact;
850
integer n13;
851
always_ff @(posedge vclk)
852
for (n13 = 0; n13 < pnSpr; n13 = n13 + 1)
853
        sprDe1[n13] <= sprDe[n13];
854
integer n22;
855
always_ff @(posedge vclk)
856
for (n22 = 0; n22 < pnSpr; n22 = n22 + 1)
857
        sprDe2[n22] <= sprDe1[n22];
858
integer n23;
859
always_ff @(posedge vclk)
860
for (n23 = 0; n23 < pnSpr; n23 = n23 + 1)
861
        sprDe3[n23] <= sprDe2[n23];
862
integer n24;
863
always_ff @(posedge vclk)
864
for (n24 = 0; n24 < pnSpr; n24 = n24 + 1)
865
        sprDe4[n24] <= sprDe3[n24];
866
integer n25;
867
always_ff @(posedge vclk)
868
for (n25 = 0; n25 < pnSpr; n25 = n25 + 1)
869
        sprDe5[n25] <= sprDe4[n25];
870
integer n26;
871
always_ff @(posedge vclk)
872
for (n26 = 0; n26 < pnSpr; n26 = n26 + 1)
873
        sprDe6[n26] <= sprDe5[n26];
874
 
875
 
876
// Detect which sprite outputs are active
877
// The sprite output is active if the current display pixel
878
// address is within the sprite's area, the sprite is enabled,
879
// and it's not a transparent pixel that's being displayed.
880
integer n14;
881
always_ff @(posedge vclk)
882
for (n14 = 0; n14 < pnSpr; n14 = n14 + 1)
883
        sproact[n14] <= sprEn[n14] && sprDe5[n14] && sprTc[n14]!=sprOut5[n14];
884
integer n15;
885
always_ff @(posedge vclk)
886
for (n15 = 0; n15 < pnSpr; n15 = n15 + 1)
887
        sprOut[n15] <= sprOut5[n15];
888
 
889
// register sprite activity flag
890
// The image combiner uses this flag to know what to do with
891
// the sprite output.
892
always_ff @(posedge vclk)
893
        outact <= |sproact;
894
 
895
// Display data comes from the active sprite with the
896
// highest display priority.
897
// Make sure that alpha blending is turned off when
898
// no sprite is active.
899
integer n16;
900
always_ff @(posedge vclk)
901
begin
902
        out <= 24'h080; // alpha blend max (and off)
903
        outplane <= 4'h0;
904
        colorBits <= 2'b00;
905
        for (n16 = pnSprm; n16 >= 0; n16 = n16 - 1)
906
                if (sproact[n16]) begin
907
                        out <= sprOut[n16];
908
                        outplane <= sprPlane[n16];
909
                        colorBits <= sprColorDepth[n16];
910
                end
911
end
912
 
913
 
914
// combine the text / graphics color output with sprite color output
915
// blend color output
916
wire [23:0] blendedColor = {
917
        fnBlend(out[7:0],zrgbIn[23:16]),                // R
918
        fnBlend(out[7:0],zrgbIn[15: 8]),                // G
919
        fnBlend(out[7:0],zrgbIn[ 7: 0])};       // B
920
 
921
 
922
always_ff @(posedge vclk)
923
if (blank_i)
924
        zrgbOut <= 0;
925
else begin
926
        if (outact) begin
927
                if (zrgbIn[31:28] > outplane) begin                     // rgb input is in front of sprite
928
                        zrgbOut <= zrgbIn;
929
                end
930
                else
931
                if (!out[23]) begin                     // a sprite is displayed without alpha blending
932
                        case(colorBits)
933
                        2'd0:   zrgbOut <= {outplane,4'h0,out[5:4],6'b0,out[3:2],6'b0,out[1:0],6'b0};
934
                        2'd1:   zrgbOut <= {outplane,4'h0,out[5:4],6'b0,out[3:2],6'b0,out[1:0],6'b0};
935
                        2'd2:   zrgbOut <= {outplane,4'h0,out[10:7],4'b0,out[6:3],4'b0,out[2:0],5'b0};
936
                        2'd3:   zrgbOut <= {outplane,4'h0,out[22:15],out[14:7],out[6:0]};
937
                        endcase
938
                end
939
                else
940
                        zrgbOut <= {outplane,4'h0,blendedColor};
941
        end
942
        else
943
                zrgbOut <= zrgbIn;
944
end
945
 
946
 
947
//--------------------------------------------------------------------
948
// Collision logic
949
//--------------------------------------------------------------------
950
 
951
// Detect when a sprite-sprite collision has occurred. The criteria
952
// for this is that a pixel from the sprite is being displayed, while
953
// there is a pixel from another sprite that could be displayed at the
954
// same time.
955
 
956
//--------------------------------------------------------------------
957
// Note this case has to be modified for the number of sprites
958
//--------------------------------------------------------------------
959
integer m1;
960
always_comb
961
begin
962
        sprCollision = sproact!=32'd0;
963
        for (m1 = 0; m1 < pnSpr; m1 = m1 + 1)
964
                sprCollision = sprCollision && !(sproact == (32'd1 << m1));
965
end
966
 
967
// Detect when a sprite-background collision has occurred
968
integer n31;
969
always_comb
970
for (n31 = 0; n31 < pnSpr; n31 = n31 + 1)
971
        bkCollision[n31] <=
972
                sproact[n31] && zrgbIn[31:28]==sprPlane[n31];
973
 
974
// Load the sprite collision register. This register continually
975
// accumulates collision bits until reset by reading the register.
976
// Set the collision IRQ on the first collision and don't set it
977
// again until after the collision register has been read.
978
always @(posedge vclk)
979
if (rst_i) begin
980
        sprSprIRQPending1 <= 0;
981
        sprSprCollision1 <= 0;
982
        sprSprIRQ1 <= 0;
983
end
984
else if (sprCollision) begin
985
        // isFirstCollision
986
        if ((sprSprCollision1==0)||(cs_regs && adr_i[9:2]==8'b11110010)) begin
987
                sprSprIRQPending1 <= 1;
988
                sprSprIRQ1 <= sprSprIe;
989
                sprSprCollision1 <= sproact;
990
        end
991
        else
992
                sprSprCollision1 <= sprSprCollision1|sproact;
993
end
994
else if (cs_regs && adr_i[9:2]==8'b11110010) begin
995
        sprSprCollision1 <= 0;
996
        sprSprIRQPending1 <= 0;
997
        sprSprIRQ1 <= 0;
998
end
999
 
1000
 
1001
// Load the sprite background collision register. This register
1002
// continually accumulates collision bits until reset by reading
1003
// the register.
1004
// Set the collision IRQ on the first collision and don't set it
1005
// again until after the collision register has been read.
1006
// Note the background collision indicator is externally supplied,
1007
// it will come from the color processing logic.
1008
always @(posedge vclk)
1009
if (rst_i) begin
1010
        sprBkIRQPending1 <= 0;
1011
        sprBkCollision1 <= 0;
1012
        sprBkIRQ1 <= 0;
1013
end
1014
else if (|bkCollision) begin
1015
        // Is the register being cleared at the same time
1016
        // a collision occurss ?
1017
        // isFirstCollision
1018
        if ((sprBkCollision1==0) || (cs_regs && adr_i[9:2]==8'b11110011)) begin
1019
                sprBkIRQ1 <= sprBkIe;
1020
                sprBkCollision1 <= bkCollision;
1021
                sprBkIRQPending1 <= 1;
1022
        end
1023
        else
1024
                sprBkCollision1 <= sprBkCollision1|bkCollision;
1025
end
1026
else if (cs_regs && adr_i[9:2]==8'b11110011) begin
1027
        sprBkCollision1 <= 0;
1028
        sprBkIRQPending1 <= 0;
1029
        sprBkIRQ1 <= 0;
1030
end
1031
 
1032
endmodule
1033
 
1034
/*
1035
module SpriteRam32 (
1036
        clka, adra, dia, doa, cea, wea,
1037
        clkb, adrb, dib, dob, ceb, web
1038
);
1039
input clka;
1040
input [9:0] adra;
1041
input [31:0] dia;
1042
output [31:0] doa;
1043
input cea;
1044
input wea;
1045
input clkb;
1046
input [9:0] adrb;
1047
input [31:0] dib;
1048
output [31:0] dob;
1049
input ceb;
1050
input web;
1051
 
1052
reg [31:0] mem [0:1023];
1053
reg [9:0] radra;
1054
reg [9:0] radrb;
1055
 
1056
always @(posedge clka)  if (cea) radra <= adra;
1057
always @(posedge clkb)  if (ceb) radrb <= adrb;
1058
assign doa = mem [radra];
1059
assign dob = mem [radrb];
1060
always @(posedge clka)
1061
        if (cea & wea) mem[adra] <= dia;
1062
always @(posedge clkb)
1063
        if (ceb & web) mem[adrb] <= dib;
1064
 
1065
endmodule
1066
 
1067
*/

powered by: WebSVN 2.1.0

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