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

Subversion Repositories theia_gpu

[/] [theia_gpu/] [branches/] [new_alu/] [src/] [Collaterals.v] - Blame information for rev 209

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 209 diegovalve
`ifndef COLLATERALS_V
2
`define COLLATERALS_V
3
 
4
`timescale 1ns / 1ps
5
`include "aDefinitions.v"
6
/**********************************************************************************
7
Theia, Ray Cast Programable graphic Processing Unit.
8
Copyright (C) 2010  Diego Valverde (diego.valverde.g@gmail.com)
9
 
10
This program is free software; you can redistribute it and/or
11
modify it under the terms of the GNU General Public License
12
as published by the Free Software Foundation; either version 2
13
of the License, or (at your option) any later version.
14
 
15
This program is distributed in the hope that it will be useful,
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
GNU General Public License for more details.
19
 
20
You should have received a copy of the GNU General Public License
21
along with this program; if not, write to the Free Software
22
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23
 
24
***********************************************************************************/
25
 
26
//----------------------------------------------------
27
module FFD_POSEDGE_SYNCRONOUS_RESET # ( parameter SIZE=`WIDTH )
28
(
29
        input wire                              Clock,
30
        input wire                              Reset,
31
        input wire                              Enable,
32
        input wire [SIZE-1:0]    D,
33
        output reg [SIZE-1:0]    Q
34
);
35
 
36
 
37
always @ (posedge Clock)
38
begin
39
        if ( Reset )
40
                Q <= {SIZE{1'b0}};
41
        else
42
        begin
43
                if (Enable)
44
                        Q <= D;
45
        end
46
 
47
end//always
48
 
49
endmodule
50
//------------------------------------------------
51
module PULSE
52
(
53
input wire                              Clock,
54
input wire                              Reset,
55
input wire                              Enable,
56
input wire              D,
57
output wire          Q
58
);
59
 
60
wire wDelay;
61
FFD_POSEDGE_SYNCRONOUS_RESET # (1) FFD (Clock,Reset,Enable,D,wDelay);
62
 
63
assign Q = (Enable) ?  (D ^ wDelay) & D: 1'b0;
64
 
65
endmodule
66
//------------------------------------------------
67
module ADDER # (parameter SIZE=`WIDTH)
68
(
69
input wire Clock,
70
input wire Reset,
71
input wire iTrigger,
72
input wire [SIZE-1:0] iA,iB,
73
output wire [SIZE-1:0] oR,
74
output wire            oDone
75
);
76
wire [SIZE-1:0] wR,wR_Delay;
77
assign wR = iA + iB;
78
 
79
FFD_POSEDGE_SYNCRONOUS_RESET # (1) FFD0 (Clock,Reset,1'b1,iTrigger,oDone);
80
 
81
FFD_POSEDGE_SYNCRONOUS_RESET # (SIZE) FFD (Clock,Reset,iTrigger,wR,wR_Delay);
82
assign oR = wR_Delay;
83
 
84
endmodule
85
 
86
//------------------------------------------------
87
module AND # (parameter SIZE=`WIDTH)
88
(
89
input wire Clock,
90
input wire Reset,
91
input wire iTrigger,
92
input wire [SIZE-1:0] iA,iB,
93
output wire [SIZE-1:0] oR,
94
output wire            oDone
95
);
96
wire [SIZE-1:0] wR,wR_Delay;
97
assign wR = iA & iB;
98
 
99
FFD_POSEDGE_SYNCRONOUS_RESET # (1) FFD0 (Clock,Reset,1'b1,iTrigger,oDone);
100
 
101
 
102
FFD_POSEDGE_SYNCRONOUS_RESET #  (SIZE) FFD (Clock,Reset,iTrigger,wR,wR_Delay);
103
assign oR = wR_Delay;
104
 
105
endmodule
106
//------------------------------------------------
107
module UPCOUNTER_POSEDGE # (parameter SIZE=`WIDTH)
108
(
109
input wire Clock, Reset,
110
input wire [SIZE-1:0] Initial,
111
input wire Enable,
112
output reg [SIZE-1:0] Q
113
);
114
 
115
 
116
  always @(posedge Clock )
117
  begin
118
      if (Reset)
119
        Q <= Initial;
120
      else
121
                begin
122
                if (Enable)
123
                        Q <= Q + 1;
124
 
125
                end
126
  end
127
 
128
endmodule
129
 
130
//----------------------------------------------------------------------
131
module DECODER_ONEHOT_2_BINARY
132
(
133
input wire [4:0] iIn,
134
output reg[4:0] oOut
135
);
136
 
137
always @ (*)
138
begin
139
        case (iIn)
140
                5'b00000: oOut = 0;
141
                5'b00001: oOut = 1;
142
                5'b00010: oOut = 2;
143
                5'b00100: oOut = 3;
144
                5'b01000: oOut = 4;
145
                5'b10000: oOut = 5;
146
        default:
147
                oOut = 0;
148
        endcase
149
end
150
endmodule
151
//----------------------------------------------------------------------
152
 
153
module SELECT_1_TO_N # ( parameter SEL_WIDTH=4, parameter OUTPUT_WIDTH=16 )
154
 (
155
 input wire [SEL_WIDTH-1:0] Sel,
156
 input wire  En,
157
 output wire [OUTPUT_WIDTH-1:0] O
158
 );
159
 
160
reg[OUTPUT_WIDTH-1:0] shift;
161
 
162
always @ ( * )
163
begin
164
        if (~En)
165
                shift = 1;
166
        else
167
                shift = (1 <<   Sel);
168
 
169
 
170
end
171
 
172
assign O = ( ~En ) ? 0 : shift ;
173
 
174
//assign O = En & (1 << Sel);
175
 
176
endmodule
177
 
178
//----------------------------------------------------------------------
179
module MUXFULLPARALELL_GENERIC #(parameter  WIDTH = `WIDTH, parameter  CHANNELS = 4, parameter SELBITS = 2)
180
(
181
 
182
    input wire   [(CHANNELS*WIDTH)-1:0]      in_bus,
183
    input wire   [SELBITS-1:0]    sel,
184
 
185
    output wire [WIDTH-1:0]                 out
186
    );
187
 
188
genvar ig;
189
 
190
wire    [WIDTH-1:0] input_array [0:CHANNELS-1];
191
 
192
assign  out = input_array[sel];
193
 
194
generate
195
    for(ig=0; ig<CHANNELS; ig=ig+1)
196
         begin: array_assignments
197
        assign  input_array[ig] = in_bus[(ig*WIDTH)+:WIDTH];
198
    end
199
endgenerate
200
 
201
 
202
 
203
endmodule
204
//----------------------------------------------------------------------
205
module MUXFULLPARALELL_2SEL_GENERIC # ( parameter SIZE=`WIDTH )
206
 (
207
 input wire [1:0] Sel,
208
 input wire [SIZE-1:0]I1, I2, I3,I4,
209
 output reg [SIZE-1:0] O1
210
 );
211
 
212
always @( * )
213
 
214
  begin
215
 
216
    case (Sel)
217
 
218
      2'b00: O1 = I1;
219
      2'b01: O1 = I2;
220
                2'b10: O1 = I3;
221
                2'b11: O1 = I4;
222
                default: O1 = SIZE;
223
 
224
    endcase
225
 
226
  end
227
 
228
endmodule
229
//------------------------------------------------------------------------
230
module MUXFULLPARALELL_3SEL_GENERIC # ( parameter SIZE=`WIDTH )
231
 (
232
 input wire [2:0] Sel,
233
 input wire [SIZE-1:0]I1, I2, I3,I4,I5,I6,I7,I8,
234
 output reg [SIZE-1:0] O1
235
 );
236
 
237
always @( * )
238
 
239
  begin
240
 
241
    case (Sel)
242
 
243
      3'b000: O1 = I1;
244
      3'b001: O1 = I2;
245
                3'b010: O1 = I3;
246
                3'b011: O1 = I4;
247
                3'b100: O1 = I5;
248
                3'b101: O1 = I6;
249
                3'b110: O1 = I7;
250
                3'b111: O1 = I8;
251
                default: O1 = SIZE;
252
 
253
    endcase
254
 
255
  end
256
 
257
endmodule
258
//------------------------------------------------------------------------
259
module CIRCULAR_SHIFTLEFT_POSEDGE_EX # ( parameter SIZE=`WIDTH )
260
( input wire Clock,
261
  input wire Reset,
262
  input wire[SIZE-1:0] Initial,
263
  input wire      Enable,
264
  output wire[SIZE-1:0] O
265
);
266
 
267
reg [SIZE-1:0] tmp;
268
 
269
 
270
  always @(posedge Clock)
271
  begin
272
  if (Reset)
273
                tmp <= Initial;
274
        else
275
        begin
276
                if (Enable)
277
                begin
278
                        if (tmp[SIZE-1])
279
                        begin
280
                                tmp <= Initial;
281
                        end
282
                        else
283
                        begin
284
                                tmp <= tmp << 1;
285
                        end
286
                end
287
        end
288
  end
289
 
290
 
291
    assign O  = tmp;
292
endmodule
293
//------------------------------------------------
294
module MUXFULLPARALELL_3SEL_WALKINGONE # ( parameter SIZE=`WIDTH )
295
 (
296
 input wire [2:0] Sel,
297
 input wire [SIZE-1:0]I1, I2, I3,
298
 output reg [SIZE-1:0] O1
299
 );
300
 
301
always @( * )
302
 
303
  begin
304
 
305
    case (Sel)
306
 
307
      3'b001: O1 = I1;
308
      3'b010: O1 = I2;
309
      3'b100: O1 = I3;
310
      default: O1 = SIZE;
311
 
312
    endcase
313
 
314
  end
315
 
316
endmodule
317
//------------------------------------------------
318
module MUXFULLPARALELL_3SEL_EN # ( parameter SIZE=`WIDTH )
319
 (
320
 input wire [1:0] SEL,
321
 input wire [SIZE-1:0]I1, I2, I3,
322
 input wire EN,
323
 output reg [SIZE-1:0] O1
324
 );
325
 
326
always @( * )
327
 
328
  begin
329
        if (EN)
330
        begin
331
    case (SEL)
332
 
333
      2'b00: O1 = I1;
334
      2'b01: O1 = I2;
335
      2'b10: O1 = I3;
336
      default: O1 = SIZE;
337
 
338
    endcase
339
        end
340
        else
341
        begin
342
                O1 = I1;
343
        end
344
  end
345
 
346
endmodule
347
//------------------------------------------------
348
module MUXFULLPARALELL_4SEL_WALKINGONE # ( parameter SIZE=`WIDTH )
349
 (
350
 input wire [2:0] Sel,
351
 input wire [SIZE-1:0]I1, I2, I3, I4,
352
 output reg [SIZE-1:0] O1
353
 );
354
 
355
always @( * )
356
 
357
  begin
358
 
359
    case (Sel)
360
 
361
      4'b0001: O1 = I1;
362
      4'b0010: O1 = I2;
363
      4'b0100: O1 = I3;
364
                4'b1000: O1 = I4;
365
      default: O1 = SIZE;
366
 
367
    endcase
368
 
369
  end
370
 
371
endmodule
372
//------------------------------------------------
373
module SHIFTLEFT_POSEDGE # ( parameter SIZE=`WIDTH )
374
( input wire Clock,
375
  input wire Reset,
376
  input wire[SIZE-1:0] Initial,
377
  input wire      Enable,
378
  output wire[SIZE-1:0] O
379
);
380
 
381
reg [SIZE-1:0] tmp;
382
 
383
 
384
  always @(posedge Clock)
385
  begin
386
  if (Reset)
387
                tmp <= Initial;
388
        else
389
        begin
390
                if (Enable)
391
                        tmp <= tmp << 1;
392
        end
393
  end
394
 
395
 
396
    assign O  = tmp;
397
endmodule
398
//------------------------------------------------
399
//------------------------------------------------
400
module CIRCULAR_SHIFTLEFT_POSEDGE # ( parameter SIZE=`WIDTH )
401
( input wire Clock,
402
  input wire Reset,
403
  input wire[SIZE-1:0] Initial,
404
  input wire      Enable,
405
  output wire[SIZE-1:0] O
406
);
407
 
408
reg [SIZE-1:0] tmp;
409
 
410
 
411
  always @(posedge Clock)
412
  begin
413
  if (Reset || tmp[SIZE-1])
414
                tmp <= Initial;
415
        else
416
        begin
417
                if (Enable)
418
                        tmp <= tmp << 1;
419
        end
420
  end
421
 
422
 
423
    assign O  = tmp;
424
endmodule
425
//-----------------------------------------------------------
426
/*
427
        Sorry forgot how this flop is called.
428
        Any way Truth table is this
429
 
430
        Q       S       Q_next R
431
 
432
 
433
        1       0        1                0
434
        1       1       1                0
435
        X       X       0                 1
436
 
437
        The idea is that it toggles from 0 to 1 when S = 1, but if it
438
        gets another S = 1, it keeps the output to 1.
439
*/
440
module FFToggleOnce_1Bit
441
(
442
        input wire Clock,
443
        input wire Reset,
444
        input wire Enable,
445
        input wire S,
446
        output reg Q
447
 
448
);
449
 
450
 
451
reg Q_next;
452
 
453
always @ (negedge Clock)
454
begin
455
        Q <= Q_next;
456
end
457
 
458
always @ ( posedge Clock )
459
begin
460
        if (Reset)
461
                Q_next <= 0;
462
        else if (Enable)
463
                Q_next <= (S && !Q) || Q;
464
        else
465
                Q_next <= Q;
466
end
467
endmodule
468
 
469
//-----------------------------------------------------------
470
 
471
 
472
module FFD32_POSEDGE
473
(
474
        input wire Clock,
475
        input wire[31:0] D,
476
        output reg[31:0] Q
477
);
478
 
479
        always @ (posedge Clock)
480
                Q <= D;
481
 
482
endmodule
483
 
484
//------------------------------------------------
485
module MUXFULLPARALELL_96bits_2SEL
486
 (
487
 input wire Sel,
488
 input wire [95:0]I1, I2,
489
 output reg [95:0] O1
490
 );
491
 
492
 
493
 
494
always @( * )
495
 
496
  begin
497
 
498
    case (Sel)
499
 
500
      1'b0: O1 = I1;
501
      1'b1: O1 = I2;
502
 
503
    endcase
504
 
505
  end
506
 
507
endmodule
508
 
509
//------------------------------------------------
510
module MUXFULLPARALELL_16bits_2SEL
511
 (
512
 input wire Sel,
513
 input wire [15:0]I1, I2,
514
 output reg [15:0] O1
515
 );
516
 
517
 
518
 
519
always @( * )
520
 
521
  begin
522
 
523
    case (Sel)
524
 
525
      1'b0: O1 = I1;
526
      1'b1: O1 = I2;
527
 
528
    endcase
529
 
530
  end
531
 
532
endmodule
533
 
534
//--------------------------------------------------------------
535
 
536
  module FFT1
537
  (
538
   input wire D,
539
   input wire Clock,
540
   input wire Reset ,
541
   output reg Q
542
 );
543
 
544
  always @ ( posedge Clock or posedge Reset )
545
  begin
546
 
547
        if (Reset)
548
        begin
549
    Q <= 1'b0;
550
   end
551
        else
552
        begin
553
                if (D)
554
                        Q <=  ! Q;
555
        end
556
 
557
  end//always
558
 
559
 endmodule
560
//--------------------------------------------------------------
561
/*
562
module FIFO_SYNCHRNOUS_RESET # ( parameter SIZE=`WIDTH, parameter DEPTH=16 )
563
(
564
input wire Clock,
565
input wire Reset,
566
wr_cs    , // Write chip select
567
rd_cs    , // Read chipe select
568
input wire            iData,
569
input wire            iReadEnable,
570
input wire[SIZE-1:0]  iWriteEnable,
571
output reg[SIZE-1:0]  oData,
572
output wire           oEmpy,
573
output wire           oFull
574
);
575
 
576
// FIFO constants
577
parameter DATA_WIDTH = 8;
578
parameter ADDR_WIDTH = 8;
579
parameter RAM_DEPTH = (1 << ADDR_WIDTH);
580
// Port Declarations
581
input clk ;
582
input rst ;
583
input wr_cs ;
584
input rd_cs ;
585
input rd_en ;
586
input wr_en ;
587
input [DATA_WIDTH-1:0] data_in ;
588
output full ;
589
output empty ;
590
output [DATA_WIDTH-1:0] data_out ;
591
 
592
//-----------Internal variables-------------------
593
reg [ADDR_WIDTH-1:0] wr_pointer;
594
reg [ADDR_WIDTH-1:0] rd_pointer;
595
reg [ADDR_WIDTH :0] status_cnt;
596
reg [DATA_WIDTH-1:0] data_out ;
597
wire [DATA_WIDTH-1:0] data_ram ;
598
 
599
//-----------Variable assignments---------------
600
assign full = (status_cnt == (RAM_DEPTH-1));
601
assign empty = (status_cnt == 0);
602
 
603
//-----------Code Start---------------------------
604
always @ (posedge clk or posedge rst)
605
begin : WRITE_POINTER
606
  if (rst) begin
607
    wr_pointer <= 0;
608
  end else if (wr_cs && wr_en ) begin
609
    wr_pointer <= wr_pointer + 1;
610
  end
611
end
612
 
613
always @ (posedge clk or posedge rst)
614
begin : READ_POINTER
615
  if (rst) begin
616
    rd_pointer <= 0;
617
  end else if (rd_cs && rd_en ) begin
618
    rd_pointer <= rd_pointer + 1;
619
  end
620
end
621
 
622
always  @ (posedge clk or posedge rst)
623
begin : READ_DATA
624
  if (rst) begin
625
    data_out <= 0;
626
  end else if (rd_cs && rd_en ) begin
627
    data_out <= data_ram;
628
  end
629
end
630
 
631
always @ (posedge clk or posedge rst)
632
begin : STATUS_COUNTER
633
  if (rst) begin
634
    status_cnt <= 0;
635
  // Read but no write.
636
  end else if ((rd_cs && rd_en) && !(wr_cs && wr_en)
637
                && (status_cnt != 0)) begin
638
    status_cnt <= status_cnt - 1;
639
  // Write but no read.
640
  end else if ((wr_cs && wr_en) && !(rd_cs && rd_en)
641
               && (status_cnt != RAM_DEPTH)) begin
642
    status_cnt <= status_cnt + 1;
643
  end
644
end
645
 
646
ram_dp_ar_aw #(DATA_WIDTH,ADDR_WIDTH)DP_RAM (
647
.address_0 (wr_pointer) , // address_0 input
648
.data_0    (data_in)    , // data_0 bi-directional
649
.cs_0      (wr_cs)      , // chip select
650
.we_0      (wr_en)      , // write enable
651
.oe_0      (1'b0)       , // output enable
652
.address_1 (rd_pointer) , // address_q input
653
.data_1    (data_ram)   , // data_1 bi-directional
654
.cs_1      (rd_cs)      , // chip select
655
.we_1      (1'b0)       , // Read enable
656
.oe_1      (rd_en)        // output enable
657
);
658
 
659
endmodule
660
*/
661
 
662
 
663
 module sync_fifo #(  parameter DATA_WIDTH = 8, parameter DEPTH = 8 )
664
(
665
 
666
 input wire [DATA_WIDTH-1:0]  din,
667
 input wire wr_en,
668
 input wire rd_en,
669
 output wire[DATA_WIDTH-1:0] dout,
670
 output reg full,
671
 output reg empty,
672
 input wire clk,
673
 input wire reset
674
 
675
);
676
 
677
 
678
 
679
function integer log2;
680
 input integer n;
681
 begin
682
 log2 = 0;
683
 while(2**log2 < n) begin
684
 log2=log2+1;
685
 end
686
 end
687
endfunction
688
 
689
parameter ADDR_WIDTH = log2(DEPTH);
690
reg   [ADDR_WIDTH : 0]     rd_ptr; // note MSB is not really address
691
reg   [ADDR_WIDTH : 0]     wr_ptr; // note MSB is not really address
692
wire  [ADDR_WIDTH-1 : 0]  wr_loc;
693
wire  [ADDR_WIDTH-1 : 0]  rd_loc;
694
reg   [DATA_WIDTH-1 : 0]  mem[DEPTH-1 : 0];
695
 
696
 
697
assign wr_loc = wr_ptr[ADDR_WIDTH-1 : 0];
698
assign rd_loc = rd_ptr[ADDR_WIDTH-1 : 0];
699
 
700
always @(posedge clk) begin
701
 if(reset) begin
702
 wr_ptr <= 'h0;
703
 rd_ptr <= 'h0;
704
end // end if
705
 
706
else begin
707
 if(wr_en & (~full))begin
708
 wr_ptr <= wr_ptr+1;
709
 end
710
 if(rd_en & (~empty))
711
 rd_ptr <= rd_ptr+1;
712
 end //end else
713
 
714
end//end always
715
 
716
 
717
 
718
//empty if all the bits of rd_ptr and wr_ptr are the same.
719
 
720
//full if all bits except the MSB are equal and MSB differes
721
 
722
always @(rd_ptr or wr_ptr)begin
723
 
724
 //default catch-alls
725
 
726
 empty <= 1'b0;
727
 
728
 full  <= 1'b0;
729
 
730
 if(rd_ptr[ADDR_WIDTH-1:0]==wr_ptr[ADDR_WIDTH-1:0])begin
731
 
732
 if(rd_ptr[ADDR_WIDTH]==wr_ptr[ADDR_WIDTH])
733
 
734
 empty <= 1'b1;
735
 
736
 else
737
 
738
 full  <= 1'b1;
739
 
740
 end//end if
741
 
742
end//end always
743
 
744
 
745
 
746
always @(posedge clk) begin
747
 
748
 if (wr_en)
749
 
750
 mem[wr_loc] <= din;
751
 
752
end //end always
753
 
754
assign dout = mem[rd_loc];//rd_en ? mem[rd_loc]:'h0;
755
 
756
endmodule
757
 
758
//---------------------------------------------------------------------
759
 
760
/*
761
Synchronous memory blocks have two independent address ports, allowing
762
for operations on two unique addresses simultaneously. A read operation and a write
763
operation can share the same port if they share the same address.
764
In the synchronous RAM block architecture, there is no priority between the two
765
ports. Therefore, if you write to the same location on both ports at the same time, the
766
result is indeterminate in the device architecture.
767
When a read and write operation occurs on the same port for
768
the same address, the new data being written to the memory is read. When a read and
769
write operation occurs on different ports for the same address, the old data in the
770
memory is read. Simultaneous writes to the same location on both ports results in
771
indeterminate behavior.
772
 
773
*/
774
module RAM_DUAL_READ_DUAL_WRITE_PORT  # ( parameter DATA_WIDTH = 8, parameter ADDR_WIDTH = 6 )
775
(
776
input wire [(DATA_WIDTH-1):0] data_a, data_b,
777
input wire [(ADDR_WIDTH-1):0] addr_a, addr_b,
778
input wire we_a, we_b, clk,
779
output reg [(DATA_WIDTH-1):0] q_a, q_b
780
);
781
 
782
 
783
// Declare the RAM variable
784
reg [DATA_WIDTH-1:0] ram[2**ADDR_WIDTH-1:0];
785
        always @ (posedge clk)
786
        begin // Port A
787
                if (we_a)
788
                begin
789
                        ram[addr_a] <= data_a;
790
                        q_a <= data_a;
791
                end
792
                else
793
                        q_a <= ram[addr_a];
794
        end
795
 
796
        always @ (posedge clk)
797
        begin // Port b
798
                if (we_b)
799
                begin
800
                        ram[addr_b] <= data_b;
801
                        q_b <= data_b;
802
                end
803
                else
804
                        q_b <= ram[addr_b];
805
        end
806
endmodule
807
 
808
 
809
module RAM_QUAD_PORT  # ( parameter DATA_WIDTH = 8, parameter ADDR_WIDTH = 6 )
810
(
811
input wire [(DATA_WIDTH-1):0] data_a, data_b,
812
input wire [(ADDR_WIDTH-1):0] waddr_a, waddr_b,
813
input wire [(ADDR_WIDTH-1):0] raddr_a, raddr_b,
814
input wire we_a, we_b, clk,
815
output reg [(DATA_WIDTH-1):0] q_a, q_b
816
);
817
 
818
 
819
// Declare the RAM variable
820
reg [DATA_WIDTH-1:0] ram[2**ADDR_WIDTH-1:0];
821
        always @ (posedge clk)
822
        begin // Port A
823
                if (we_a)
824
                begin
825
                        ram[waddr_a] <= data_a;
826
                        q_a <= data_a;
827
                end
828
                else
829
                        q_a <= ram[waddr_a];
830
        end
831
 
832
        always @ (posedge clk)
833
        begin // Port B
834
                if (we_b)
835
                begin
836
                        ram[waddr_b] <= data_b;
837
                        q_b <= data_b;
838
                end
839
                else
840
                        q_b <= ram[waddr_b];
841
        end
842
 
843
 
844
endmodule
845
//-------------------------------------------------------------------------------
846
//----------------------------------------------------
847
   // A four level, round-robin arbiter. This was
848
   // orginally coded by WD Peterson in VHDL.
849
   //----------------------------------------------------
850
    module ROUND_ROBIN_ARBITER (
851
      clk,
852
      rst,
853
                req4,
854
      req3,
855
      req2,
856
     req1,
857
     req0,
858
          gnt4,
859
     gnt3,
860
     gnt2,
861
     gnt1,
862
     gnt0
863
   );
864
   // --------------Port Declaration----------------------- 
865
   input           clk;
866
   input           rst;
867
        input           req4;
868
   input           req3;
869
   input           req2;
870
   input           req1;
871
   input           req0;
872
        output          gnt4;
873
   output          gnt3;
874
   output          gnt2;
875
   output          gnt1;
876
   output          gnt0;
877
 
878
   //--------------Internal Registers----------------------
879
   wire    [2:0]   gnt       ;
880
   wire            comreq    ;
881
   wire            beg       ;
882
   wire   [2:0]    lgnt      ;
883
   wire            lcomreq   ;
884
   reg             lgnt0     ;
885
   reg             lgnt1     ;
886
   reg             lgnt2     ;
887
   reg             lgnt3     ;
888
        reg             lgnt4     ;
889
   reg             lasmask   ;
890
   reg             lmask0    ;
891
   reg             lmask1    ;
892
        reg             lmask2    ;
893
   reg             ledge     ;
894
 
895
   //--------------Code Starts Here----------------------- 
896
   always @ (posedge clk)
897
   if (rst) begin
898
     lgnt0 <= 0;
899
     lgnt1 <= 0;
900
     lgnt2 <= 0;
901
     lgnt3 <= 0;
902
          lgnt4 <= 0;
903
   end else begin
904
     lgnt0 <=(~lcomreq & ~lmask2 & ~lmask1 & ~lmask0 & ~req4 & ~req3 & ~req2 & ~req1 & req0)
905
           | (~lcomreq & ~lmask2 & ~lmask1 &  lmask0 & ~req4 & ~req3 & ~req2 &  req0)
906
           | (~lcomreq & ~lmask2 &  lmask1 & ~lmask0 & ~req4 & ~req3 &  req0)
907
           | (~lcomreq & ~lmask2 &  lmask1 &  lmask0 & ~req4 & req0  )
908
                          | (~lcomreq &  lmask2 & ~lmask1 & ~lmask0 &  req0  )
909
           | ( lcomreq & lgnt0 );
910
     lgnt1 <=(~lcomreq & ~lmask2 & ~lmask1 & ~lmask0 &  req1)
911
           | (~lcomreq & ~lmask2 & ~lmask1 &  lmask0 & ~req4 & ~req3 & ~req2 &  req1 & ~req0)
912
           | (~lcomreq & ~lmask2 &  lmask1 & ~lmask0 & ~req4 & ~req3 &  req1 & ~req0)
913
           | (~lcomreq & ~lmask2 &  lmask1 &  lmask0 & ~req4 &  req1 & ~req0)
914
                          | (~lcomreq &  lmask2 & ~lmask1 & ~lmask0 &  req1 & ~req0)
915
           | ( lcomreq &  lgnt1);
916
     lgnt2 <=(~lcomreq & ~lmask2 & ~lmask1 & ~lmask0 &  req2  & ~req1)
917
           | (~lcomreq & ~lmask2 & ~lmask1 &  lmask0 &  req2)
918
           | (~lcomreq & ~lmask2 &  lmask1 & ~lmask0 & ~req4 & ~req3 &  req2  & ~req1 & ~req0)
919
           | (~lcomreq & ~lmask2 &  lmask1 &  lmask0 & ~req4 & req2 & ~req1 & ~req0)
920
                          | ( lcomreq &  lmask2 & ~lmask1 & ~lmask0 &  req2 & ~req1 & ~req0)
921
           | ( lcomreq &  lgnt2);
922
     lgnt3 <=(~lcomreq & ~lmask2 & ~lmask1 & ~lmask0 & ~req4 & req3  & ~req2 & ~req1)
923
           | (~lcomreq & ~lmask2 & ~lmask1 &  lmask0 & ~req4 & req3  & ~req2)
924
           | (~lcomreq & ~lmask2 &  lmask1 & ~lmask0 & ~req4 & req3)
925
                          | (~lcomreq & ~lmask2 & ~lmask2 &  lmask1 &  lmask0 &  req3)
926
           | ( lcomreq &  lmask2 & ~lmask1 & ~lmask0 & ~req4 & req3  & ~req2 & ~req1 & ~req0)
927
           | ( lcomreq & lgnt3);
928
          lgnt4 <=(~lcomreq & ~lmask2 & ~lmask1 & ~lmask0 &  req4 & ~req3  & ~req2 & ~req1 & ~req0)
929
           | (~lcomreq & ~lmask2 & ~lmask1 &  lmask0 &  req4 & ~req3  & ~req2 & ~req1 )
930
           | (~lcomreq & ~lmask2 &  lmask1 & ~lmask0 &  req4 & ~req3  & ~req2 )
931
                          | (~lcomreq & ~lmask2 &  lmask1 &  lmask0 &  req4 & ~req3 )
932
           | ( lcomreq &  lmask2 & ~lmask1 & ~lmask0 &  req4 )
933
           | ( lcomreq & lgnt3);
934
   end
935
 
936
   //----------------------------------------------------
937
   // lasmask state machine.
938
   //----------------------------------------------------
939
   assign beg = (req4 | req3 | req2 | req1 | req0) & ~lcomreq;
940
   always @ (posedge clk)
941
   begin
942
     lasmask <= (beg & ~ledge & ~lasmask);
943
     ledge   <= (beg & ~ledge &  lasmask)
944
             |  (beg &  ledge & ~lasmask);
945
   end
946
 
947
   //----------------------------------------------------
948
   // comreq logic.
949
   //----------------------------------------------------
950
   assign lcomreq =
951
                                                        ( req4 & lgnt4 )
952
                                                 | ( req3 & lgnt3 )
953
                   | ( req2 & lgnt2 )
954
                   | ( req1 & lgnt1 )
955
                   | ( req0 & lgnt0 );
956
 
957
   //----------------------------------------------------
958
   // Encoder logic.
959
   //----------------------------------------------------
960
   assign  lgnt =  {lgnt4,(lgnt3 | lgnt2),(lgnt3 | lgnt1)};
961
 
962
   //----------------------------------------------------
963
   // lmask register.
964
  //----------------------------------------------------
965
  always @ (posedge clk )
966
  if( rst ) begin
967
         lmask2 <= 0;
968
    lmask1 <= 0;
969
    lmask0 <= 0;
970
  end else if(lasmask) begin
971
    lmask2 <= lgnt[2];
972
    lmask1 <= lgnt[1];
973
    lmask0 <= lgnt[0];
974
  end else begin
975
         lmask2 <= lmask2;
976
    lmask1 <= lmask1;
977
    lmask0 <= lmask0;
978
  end
979
 
980
  assign comreq = lcomreq;
981
  assign gnt    = lgnt;
982
  //----------------------------------------------------
983
  // Drive the outputs
984
  //----------------------------------------------------
985
  assign gnt4   = lgnt4;
986
  assign gnt3   = lgnt3;
987
  assign gnt2   = lgnt2;
988
  assign gnt1   = lgnt1;
989
  assign gnt0   = lgnt0;
990
 
991
  endmodule
992
//-------------------------------------------------------------------------------
993
module ROUND_ROBIN_5_ENTRIES
994
(
995
input wire Clock,
996
input wire Reset,
997
input wire iRequest0,
998
input wire iRequest1,
999
input wire iRequest2,
1000
input wire iRequest3,
1001
input wire iRequest4,
1002
output wire oGrant0,
1003
output wire oGrant1,
1004
output wire oGrant2,
1005
output wire oGrant3,
1006
output wire oGrant4,
1007
output wire oPriorityGrant
1008
 
1009
);
1010
wire wMaks2,wMaks1,wMaks0;
1011
wire wGrant0,wGrant1,wGrant2,wGrant3,wGrant4;
1012
 
1013
assign wGrant0 =
1014
                   (wMaks2 &  ~wMaks1 &  ~wMaks0 &  iRequest0 &  ~iRequest4 )
1015
                                                |(~wMaks2 &  wMaks1 &  wMaks0 &  iRequest0 &  ~iRequest4 & ~iRequest3 )
1016
                                                |(~wMaks2 &  wMaks1 & ~wMaks0 &  iRequest0 &  ~iRequest4 & ~iRequest3 & ~iRequest2 )
1017
                                                |(~wMaks2 & ~wMaks1 &  wMaks0 &  iRequest0 &  ~iRequest4 & ~iRequest3 & ~iRequest2 & ~iRequest1 )
1018
                                                |(~wMaks2 & ~wMaks1 & ~wMaks0 &  iRequest0 );
1019
 
1020
 
1021
assign wGrant1 =
1022
                   (wMaks2 &  ~wMaks1 & ~wMaks0 &   iRequest1 & ~iRequest0 &  ~iRequest4)
1023
                                                |(~wMaks2 &  wMaks1 &  wMaks0 &   iRequest1 & ~iRequest0 &  ~iRequest4 & ~iRequest3 )
1024
                                                |(~wMaks2 &  wMaks1 & ~wMaks0 &   iRequest1 & ~iRequest0 &  ~iRequest4 & ~iRequest3 & ~iRequest2 )
1025
                                                |(~wMaks2 & ~wMaks1 &  wMaks0 &   iRequest1 )
1026
                                                |(~wMaks2 & ~wMaks1 & ~wMaks0 &   iRequest1 & ~iRequest0);
1027
 
1028
assign wGrant2 =
1029
                   (wMaks2 &  ~wMaks1 &  ~wMaks0 &  iRequest2 & ~iRequest1 & ~iRequest0 &  ~iRequest4 )
1030
                                                |(~wMaks2 &  wMaks1 &  wMaks0 &  iRequest2 & ~iRequest1 & ~iRequest0 &  ~iRequest4 & ~iRequest3 )
1031
                                                |(~wMaks2 &  wMaks1 & ~wMaks0 &  iRequest2 )
1032
                                                |(~wMaks2 & ~wMaks1 &  wMaks0 &  iRequest2 & ~iRequest1 )
1033
                                                |(~wMaks2 & ~wMaks1 & ~wMaks0 &  iRequest2 & ~iRequest1 & ~iRequest0 );
1034
 
1035
assign wGrant3 =
1036
                   (wMaks2 &  ~wMaks1 &  ~wMaks0 &  iRequest3 & ~iRequest2 & ~iRequest1 & ~iRequest0 &  ~iRequest4 )
1037
                                                |(~wMaks2 &  wMaks1 &  wMaks0 &  iRequest3 )
1038
                                                |(~wMaks2 &  wMaks1 & ~wMaks0 &  iRequest3 & ~iRequest2 )
1039
                                                |(~wMaks2 & ~wMaks1 &  wMaks0 &  iRequest3 & ~iRequest2 & ~iRequest1 )
1040
                                                |(~wMaks2 & ~wMaks1 & ~wMaks0 &  iRequest3 & ~iRequest2 & ~iRequest1 & ~iRequest0 );
1041
 
1042
assign wGrant4 =
1043
                   ( wMaks2 &  ~wMaks1 &  ~wMaks0 &  iRequest4 )
1044
                                                |(~wMaks2 &  wMaks1 &  wMaks0 &  iRequest4 & ~iRequest3 )
1045
                                                |(~wMaks2 &  wMaks1 & ~wMaks0 &  iRequest4 & ~iRequest3 & ~iRequest2 )
1046
                                                |(~wMaks2 & ~wMaks1 &  wMaks0 &  iRequest4 & ~iRequest3 & ~iRequest2 & ~iRequest1 )
1047
                                                |(~wMaks2 & ~wMaks1 & ~wMaks0 &  iRequest4 & ~iRequest3 & ~iRequest2 & ~iRequest1 & ~iRequest0 );
1048
 
1049
 
1050
assign oPriorityGrant = wGrant0;
1051
 
1052
FFD_POSEDGE_SYNCRONOUS_RESET # ( 1 ) FFD0
1053
(       Clock, Reset, 1'b1 , wGrant0, oGrant0);
1054
 
1055
FFD_POSEDGE_SYNCRONOUS_RESET # ( 1 ) FFD1
1056
(       Clock, Reset, 1'b1 , wGrant1,  oGrant1 );
1057
 
1058
FFD_POSEDGE_SYNCRONOUS_RESET # ( 1 ) FFD2
1059
(       Clock, Reset, 1'b1 , wGrant2, oGrant2 );
1060
 
1061
FFD_POSEDGE_SYNCRONOUS_RESET # ( 1 ) FFD3
1062
(       Clock, Reset, 1'b1 , wGrant3, oGrant3 );
1063
 
1064
FFD_POSEDGE_SYNCRONOUS_RESET # ( 1 ) FFD4
1065
(       Clock, Reset, 1'b1 , wGrant4, oGrant4 );
1066
 
1067
 
1068
 
1069
 
1070
reg [4:0]  rCurrentState, rNextState;
1071
//Next states logic and Reset sequence
1072
always @(posedge Clock )
1073
  begin
1074
 
1075
    if (Reset )
1076
                rCurrentState <= 0;
1077
    else
1078
                rCurrentState <= rNextState;
1079
 
1080
end
1081
reg[2:0] rMask;
1082
 
1083
assign wMaks0 = rMask[0];
1084
assign wMaks1 = rMask[1];
1085
assign wMaks2 = rMask[2];
1086
 
1087
always @ ( * )
1088
begin
1089
        case (rCurrentState)
1090
        //--------------------------------------
1091
        0:
1092
        begin
1093
                rMask = 3'd0;
1094
                rNextState = 1;
1095
        end
1096
        1:
1097
        begin
1098
                rMask = 3'd1;
1099
                rNextState = 2;
1100
        end
1101
        2:
1102
        begin
1103
                rMask = 3'd2;
1104
                rNextState = 3;
1105
        end
1106
        3:
1107
        begin
1108
                rMask = 3'd3;
1109
                rNextState = 4;
1110
        end
1111
        4:
1112
        begin
1113
                rMask = 3'd4;
1114
                rNextState = 0;
1115
        end
1116
        endcase
1117
end
1118
        /*
1119
UPCOUNTER_POSEDGE # (3) UP1
1120
(
1121
.Clock( Clock ),
1122
.Reset( Reset ),
1123
.Initial( 3'b0 ),
1124
.Enable( 1'b1 ),
1125
.Q({wMaks2,wMaks1,wMaks0})
1126
);
1127
*/
1128
 
1129
 
1130
endmodule
1131
//-------------------------------------------------------------------------------
1132
 
1133
`endif

powered by: WebSVN 2.1.0

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