[1819] | 1 | //********************************************************************************************** |
---|
| 2 | // File: fec_encoder.v |
---|
| 3 | // Author: Yang Sun (ysun@rice.edu) |
---|
| 4 | // Created: 2010.10.8 |
---|
| 5 | //********************************************************************************************** |
---|
| 6 | module fec_encoder ( |
---|
| 7 | clk , // I, clock |
---|
| 8 | ce , // I, clock enable |
---|
| 9 | nrst , // I, n reset |
---|
| 10 | start , // I, start pulse |
---|
| 11 | coding_en , // I, coding enable |
---|
| 12 | pkt_done , // I, pkt transmit done |
---|
| 13 | fec_rd , // I, fec data request |
---|
| 14 | info_data , // I, info data |
---|
| 15 | info_scram , // I, info data scrabled |
---|
| 16 | info_len , // I, number of payload |
---|
| 17 | codeword_len , // O, number of coded data in bytes |
---|
| 18 | info_rd , // O, read |
---|
| 19 | info_raddr , // O, TX byte address |
---|
| 20 | fec_data // O, encoded data in bytes |
---|
| 21 | ) ; |
---|
| 22 | |
---|
| 23 | input clk ; |
---|
| 24 | input ce ; |
---|
| 25 | input nrst ; |
---|
| 26 | input start ; |
---|
| 27 | input coding_en ; |
---|
| 28 | input pkt_done ; |
---|
| 29 | input fec_rd ; |
---|
| 30 | input [7:0] info_data ; |
---|
| 31 | input [7:0] info_scram ; |
---|
| 32 | input [15:0] info_len ; |
---|
| 33 | output [15:0] codeword_len ; |
---|
| 34 | output info_rd ; |
---|
| 35 | output [13:0] info_raddr ; |
---|
| 36 | output [7:0] fec_data ; |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | //============================ |
---|
| 40 | //Internal signal |
---|
| 41 | //============================ |
---|
| 42 | reg in_enc ; |
---|
| 43 | wire ff_half_full ; |
---|
| 44 | wire [7:0] ff_wdata ; |
---|
| 45 | wire ff_wr ; |
---|
| 46 | wire ff_rd ; |
---|
| 47 | wire [7:0] ff_rdata ; |
---|
| 48 | wire enc_done ; |
---|
| 49 | wire ffdata_req ; |
---|
| 50 | reg ffdata_req_d ; |
---|
| 51 | reg ffdata_req_dd ; |
---|
| 52 | reg [13:0] addr_out ; |
---|
| 53 | |
---|
| 54 | wire [7:0] sff_din_cc ; |
---|
| 55 | wire sff_empty_cc ; |
---|
| 56 | wire sff_full_cc ; |
---|
| 57 | wire sff_rd_cc ; |
---|
| 58 | wire sff_wr_cc ; |
---|
| 59 | wire [3:0] sff_dout_cc ; |
---|
| 60 | |
---|
| 61 | wire cc_vin ; |
---|
| 62 | wire [3:0] cc_din ; |
---|
| 63 | wire [7:0] cc_dout ; |
---|
| 64 | wire [7:0] cc_mask ; |
---|
| 65 | wire [7:0] cc_dpunct ; |
---|
| 66 | wire [3:0] n_cc_dpunct ; |
---|
| 67 | |
---|
| 68 | wire sff_empty_qam ; |
---|
| 69 | wire sff_full_qam ; |
---|
| 70 | wire sff_rd_qam ; |
---|
| 71 | wire sff_wr_qam ; |
---|
| 72 | wire [7:0] sff_dout_qam ; |
---|
| 73 | wire [7:0] sff_din_qam ; |
---|
| 74 | wire [3:0] sff_nsin_qam ; |
---|
| 75 | wire coding_en ; |
---|
| 76 | wire [1:0] cc_rate_mux ; |
---|
| 77 | reg [1:0] cc_rate ; |
---|
| 78 | |
---|
| 79 | reg [5:0] cc_cnt ; |
---|
| 80 | wire in_baserate_cc ; |
---|
| 81 | reg [7:0] fec_data_i ; |
---|
| 82 | wire bypass ; |
---|
| 83 | |
---|
| 84 | reg [15:0] pkt_len ; |
---|
| 85 | reg [15:0] info_len_lat ; |
---|
| 86 | reg [15:0] payload_len ; |
---|
| 87 | wire [15:0] payload_len_i ; |
---|
| 88 | |
---|
| 89 | wire [10:0] coe ; |
---|
| 90 | wire [26:0] tmp_mult ; |
---|
| 91 | reg [15:0] payload_coded ; |
---|
| 92 | |
---|
| 93 | //============================ |
---|
| 94 | // Main RTL |
---|
| 95 | //============================ |
---|
| 96 | assign info_raddr = addr_out ; |
---|
| 97 | assign fec_data = fec_data_i ; |
---|
| 98 | assign info_rd = ffdata_req_d ; |
---|
| 99 | assign codeword_len = pkt_len ; |
---|
| 100 | |
---|
| 101 | always @ (posedge clk or negedge nrst) |
---|
| 102 | if(~nrst) |
---|
| 103 | fec_data_i <= 0 ; |
---|
| 104 | else if(ce) |
---|
| 105 | begin |
---|
| 106 | if(start) |
---|
| 107 | fec_data_i <= 0 ; |
---|
| 108 | else if(fec_rd) |
---|
| 109 | fec_data_i <= sff_dout_qam ; |
---|
| 110 | end |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | always @ (posedge clk or negedge nrst) |
---|
| 114 | if (~nrst) |
---|
| 115 | in_enc <= 1'b0 ; |
---|
| 116 | else if (ce) |
---|
| 117 | begin |
---|
| 118 | if (start) |
---|
| 119 | in_enc <= 1'b1 ; |
---|
| 120 | else if (pkt_done) |
---|
| 121 | in_enc <= 1'b0 ; |
---|
| 122 | end |
---|
| 123 | |
---|
| 124 | assign ffdata_req = in_enc & ~ff_half_full ; |
---|
| 125 | always @ (posedge clk or negedge nrst) |
---|
| 126 | if (~nrst) |
---|
| 127 | {ffdata_req_dd, ffdata_req_d} <= 2'b00 ; |
---|
| 128 | else if (ce) |
---|
| 129 | {ffdata_req_dd, ffdata_req_d} <= {ffdata_req_d, ffdata_req} ; |
---|
| 130 | |
---|
| 131 | always @ (posedge clk or negedge nrst) |
---|
| 132 | if (~nrst) |
---|
| 133 | addr_out <= 0 ; |
---|
| 134 | else if (ce) |
---|
| 135 | begin |
---|
| 136 | if (start) |
---|
| 137 | addr_out <= 0 ; |
---|
| 138 | else if (ffdata_req) |
---|
| 139 | addr_out <= addr_out +1 ; |
---|
| 140 | end |
---|
| 141 | |
---|
| 142 | |
---|
| 143 | assign ff_wr = ffdata_req_dd ; |
---|
| 144 | assign ff_wdata = info_scram ; |
---|
| 145 | assign ff_rd = sff_wr_cc ; |
---|
| 146 | |
---|
| 147 | //-- 8x8 Input FIFO |
---|
| 148 | fifo_async_rd #(8, 8, 3) input_fifo ( |
---|
| 149 | .clk (clk ), |
---|
| 150 | .nrst (nrst ), |
---|
| 151 | .ce (ce ), |
---|
| 152 | .reset (start ), |
---|
| 153 | .wdata (ff_wdata ), |
---|
| 154 | .wr (ff_wr ), |
---|
| 155 | .rd (ff_rd ), |
---|
| 156 | .rdata (ff_rdata ), |
---|
| 157 | .empty (ff_empty ), |
---|
| 158 | .half_full (ff_half_full ), |
---|
| 159 | .full (ff_full ) |
---|
| 160 | ) ; |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | assign sff_din_cc = ff_rdata ; |
---|
| 164 | assign sff_wr_cc = (~sff_full_cc) & (~ff_empty) & in_enc ; |
---|
| 165 | assign sff_rd_cc = sff_wr_qam ; |
---|
| 166 | |
---|
| 167 | //-- shift fifo between input fifo and CC encoder |
---|
| 168 | sfifo_8to4 cc_fifo ( |
---|
| 169 | .clk (clk ), // clock |
---|
| 170 | .ce (ce ), // clock enable |
---|
| 171 | .nrst (nrst ), // asyn reset |
---|
| 172 | .reset (start ), // sync reset |
---|
| 173 | .din (sff_din_cc ), // data input |
---|
| 174 | .dout (sff_dout_cc ), // data output |
---|
| 175 | .rd (sff_rd_cc ), // read |
---|
| 176 | .wr (sff_wr_cc ), // write |
---|
| 177 | .full (sff_full_cc ), // fifo full |
---|
| 178 | .empty (sff_empty_cc ), // fifo empty |
---|
| 179 | .nsin (4'd8 ), // num of shift in (bits) |
---|
| 180 | .nsout (4'd4 ) // num of shift out (bits) |
---|
| 181 | ) ; |
---|
| 182 | |
---|
| 183 | // get coding rate from second byte of the header |
---|
| 184 | always @ (posedge clk or negedge nrst) |
---|
| 185 | if (~nrst) |
---|
| 186 | cc_rate <= 0 ; |
---|
| 187 | else if (ce) |
---|
| 188 | begin |
---|
| 189 | if(start) |
---|
| 190 | cc_rate <= 0 ; |
---|
| 191 | else if (info_rd & (info_raddr == 3)) // read latency is 2 |
---|
| 192 | cc_rate <= info_data ; |
---|
| 193 | end |
---|
| 194 | |
---|
| 195 | assign cc_vin = sff_wr_qam ; |
---|
| 196 | assign cc_din = sff_dout_cc ; |
---|
| 197 | assign cc_rate_mux = in_baserate_cc ? 0 : cc_rate ; |
---|
| 198 | |
---|
| 199 | //-- cc_encoder |
---|
| 200 | cc_encoder cc_encoder ( |
---|
| 201 | .clk (clk ), // I, clock |
---|
| 202 | .ce (ce ), // I, clock enable |
---|
| 203 | .nrst (nrst ), // I, n reset |
---|
| 204 | .cc_start (start ), // I, start pulse |
---|
| 205 | .rate (cc_rate_mux ), // I, code rate |
---|
| 206 | .vin (cc_vin ), // I, valid in |
---|
| 207 | .din (cc_din ), // I, parallel data input. QPSK = din[3], 16-QAM = din[3:2], 64-QAM = din[3:1], 256-QAM = din[3:0] |
---|
| 208 | .vout ( ), // O, encoded data out valid |
---|
| 209 | .dout (cc_dout ), // O, arallel data output. QPSK = dout[7:6], 16-QAM = dout[7:4], 64-QAM = dout[7:2], 256-QAM = dout[7:0] |
---|
| 210 | .mask (cc_mask ) // O, mask |
---|
| 211 | ) ; |
---|
| 212 | |
---|
| 213 | always @ (posedge clk or negedge nrst) |
---|
| 214 | if (~nrst) |
---|
| 215 | cc_cnt <= 0 ; |
---|
| 216 | else if (ce) |
---|
| 217 | begin |
---|
| 218 | if(start) |
---|
| 219 | cc_cnt <= 0 ; |
---|
| 220 | else if (cc_vin & in_baserate_cc) |
---|
| 221 | cc_cnt <= cc_cnt +1 ; |
---|
| 222 | end |
---|
| 223 | |
---|
| 224 | assign in_baserate_cc = cc_cnt != 48; |
---|
| 225 | |
---|
| 226 | //-- cc_puncture |
---|
| 227 | cc_puncture cc_puncture ( |
---|
| 228 | .din (cc_dout ) , // I, data in |
---|
| 229 | .mask (cc_mask ) , // I, data in mask |
---|
| 230 | .dout (cc_dpunct ) , // O, data out |
---|
| 231 | .ndout (n_cc_dpunct) // O, number of output bits |
---|
| 232 | ) ; |
---|
| 233 | |
---|
| 234 | assign sff_rd_qam = fec_rd ; |
---|
| 235 | assign sff_wr_qam = ((~sff_full_qam) & (~sff_empty_cc)) & in_enc ; |
---|
| 236 | |
---|
| 237 | assign bypass = (~coding_en) | (cc_rate_mux == 3) ; |
---|
| 238 | assign sff_din_qam = bypass ? {sff_dout_cc, 4'd0} : cc_dpunct ; |
---|
| 239 | assign sff_nsin_qam = bypass ? 4 : n_cc_dpunct ; |
---|
| 240 | |
---|
| 241 | //-- shift fifo between cc encoder and QAM modulator |
---|
| 242 | sfifo_nto8 output_fifo ( |
---|
| 243 | .clk (clk ), // clock |
---|
| 244 | .ce (ce ), // clock enable |
---|
| 245 | .nrst (nrst ), // asyn reset |
---|
| 246 | .reset (start ), // sync reset |
---|
| 247 | .din (sff_din_qam ), // data input |
---|
| 248 | .dout (sff_dout_qam ), // data output |
---|
| 249 | .rd (sff_rd_qam ), // read |
---|
| 250 | .wr (sff_wr_qam ), // write |
---|
| 251 | .full (sff_full_qam ), // fifo full |
---|
| 252 | .empty (sff_empty_qam ), // fifo empty |
---|
| 253 | .nsin (sff_nsin_qam ), // num of shift in (bits), could be 4, 6 or 8 |
---|
| 254 | .nsout (4'd8 ) // num of shift out (bits) |
---|
| 255 | ) ; |
---|
| 256 | |
---|
| 257 | |
---|
| 258 | always @(posedge clk or negedge nrst) |
---|
| 259 | if(~nrst) |
---|
| 260 | info_len_lat <= 256 ; |
---|
| 261 | else if(ce) |
---|
| 262 | info_len_lat <= info_len ; |
---|
| 263 | |
---|
| 264 | always @(posedge clk or negedge nrst) |
---|
| 265 | if(~nrst) |
---|
| 266 | payload_len <= 0 ; |
---|
| 267 | else if(ce) |
---|
| 268 | payload_len <= info_len -24 ; |
---|
| 269 | |
---|
| 270 | assign payload_len_i = {payload_len, 1'b0} ; |
---|
| 271 | |
---|
| 272 | always @* |
---|
| 273 | begin |
---|
| 274 | pkt_len = info_len_lat ; |
---|
| 275 | if(coding_en) |
---|
| 276 | begin |
---|
| 277 | case(cc_rate_mux) |
---|
| 278 | 2'd0: pkt_len = {info_len_lat[14:0], 1'b0} ; // rate 1/2 |
---|
| 279 | 2'd1: pkt_len = payload_coded +50 ; // rate 2/3 |
---|
| 280 | 2'd2: pkt_len = payload_coded +50 ; // rate 3/4 |
---|
| 281 | 2'd3: pkt_len = info_len_lat +24 ; // rate 1 |
---|
| 282 | endcase |
---|
| 283 | end |
---|
| 284 | else |
---|
| 285 | pkt_len = info_len_lat ; |
---|
| 286 | end |
---|
| 287 | |
---|
| 288 | // 1536 = 3/4 * 2048, 1365 = 2/3 * 2048 |
---|
| 289 | assign coe = (cc_rate_mux == 1) ? 1536 : 1365 ; |
---|
| 290 | assign tmp_mult = coe * payload_len_i ; |
---|
| 291 | |
---|
| 292 | always @(posedge clk or negedge nrst) |
---|
| 293 | if(~nrst) |
---|
| 294 | payload_coded <= 256 ; |
---|
| 295 | else if (ce) |
---|
| 296 | payload_coded <= tmp_mult[26:11] ; |
---|
| 297 | |
---|
| 298 | endmodule |
---|
| 299 | |
---|
| 300 | //************************************************************** |
---|
| 301 | // File: cc_encoder.v |
---|
| 302 | // Author: Yang Sun (ysun@rice.edu) |
---|
| 303 | // Created: $ 02/11/07 |
---|
| 304 | // Des: Convolutional encoder core |
---|
| 305 | // K = 7, compliant with 802.11a |
---|
| 306 | // u0 u1 u2 u3 -> {A0,B0}, {A1,B1}, {A2,B2}, {A3,B3} |
---|
| 307 | // |
---|
| 308 | // History: $ 02/11/07, First version, K = 5 |
---|
| 309 | // $ 03/31/07, Added 256-QAM |
---|
| 310 | // $ 04/20/07, K = 7 |
---|
| 311 | // $ 04/29/07, Added ce |
---|
| 312 | // $ 11/26/07, Remove puncture |
---|
| 313 | // $ 09/27/08, Added puncture |
---|
| 314 | // $ 10/11/08, Added uncoded and BPSK support |
---|
| 315 | //************************************************************** |
---|
| 316 | module cc_encoder ( |
---|
| 317 | clk , // I, clock |
---|
| 318 | ce , // I, clock enable |
---|
| 319 | nrst , // I, n reset |
---|
| 320 | cc_start , // I, start pulse |
---|
| 321 | rate , // I, code rate, 0 = 1/2, 1 = 2/3, 2 = 3/4 |
---|
| 322 | vin , // I, valid in |
---|
| 323 | din , // I, parallel data input [3:0] |
---|
| 324 | vout , // O, encoded data out valid |
---|
| 325 | dout , // O, Parallel data output [7:0] |
---|
| 326 | mask // O, mask |
---|
| 327 | ) ; |
---|
| 328 | |
---|
| 329 | input clk ; |
---|
| 330 | input ce ; |
---|
| 331 | input nrst ; |
---|
| 332 | input cc_start ; |
---|
| 333 | input [1:0] rate ; |
---|
| 334 | input vin ; |
---|
| 335 | input [3:0] din ; |
---|
| 336 | output vout ; |
---|
| 337 | output [7:0] dout ; |
---|
| 338 | output reg [7:0] mask ; |
---|
| 339 | |
---|
| 340 | //=========================================== |
---|
| 341 | //Internal signal |
---|
| 342 | //=========================================== |
---|
| 343 | wire [3:0] u ; |
---|
| 344 | wire [7:0] v ; |
---|
| 345 | |
---|
| 346 | reg s0 ; |
---|
| 347 | reg s1 ; |
---|
| 348 | reg s2 ; |
---|
| 349 | reg s3 ; |
---|
| 350 | reg s4 ; |
---|
| 351 | reg s5 ; |
---|
| 352 | |
---|
| 353 | wire s0_next ; |
---|
| 354 | wire s1_next ; |
---|
| 355 | wire s2_next ; |
---|
| 356 | wire s3_next ; |
---|
| 357 | wire s4_next ; |
---|
| 358 | wire s5_next ; |
---|
| 359 | |
---|
| 360 | wire [3:0] A ; |
---|
| 361 | wire [3:0] B ; |
---|
| 362 | |
---|
| 363 | reg [1:0] cnt ; |
---|
| 364 | |
---|
| 365 | //=========================================== |
---|
| 366 | // Main body of code |
---|
| 367 | //=========================================== |
---|
| 368 | assign vout = vin ; |
---|
| 369 | assign dout = v ; |
---|
| 370 | |
---|
| 371 | assign A[3] = u[3] ^ s1 ^ s2 ^ s4 ^ s5 ; |
---|
| 372 | assign B[3] = u[3] ^ s0 ^ s1 ^ s2 ^ s5 ; |
---|
| 373 | |
---|
| 374 | assign A[2] = (u[2] ^ s0 ^ s1 ^ s3 ^ s4) ; |
---|
| 375 | assign B[2] = (u[2] ^ u[3] ^ s0 ^ s1 ^ s4) ; |
---|
| 376 | |
---|
| 377 | assign A[1] = (u[1] ^ u[3] ^ s0 ^ s2 ^ s3) ; |
---|
| 378 | assign B[1] = (u[1] ^ u[2] ^ u[3] ^ s0 ^ s3) ; |
---|
| 379 | |
---|
| 380 | assign A[0] = (u[0] ^ u[2] ^ u[3] ^ s1 ^ s2) ; |
---|
| 381 | assign B[0] = (u[0] ^ u[1] ^ u[2] ^ u[3] ^ s2) ; |
---|
| 382 | |
---|
| 383 | assign u = din ; |
---|
| 384 | assign v = {A[3], B[3], A[2], B[2], A[1], B[1], A[0], B[0]} ; |
---|
| 385 | |
---|
| 386 | assign s0_next = u[0] ; |
---|
| 387 | assign s1_next = u[1] ; |
---|
| 388 | assign s2_next = u[2] ; |
---|
| 389 | assign s3_next = u[3] ; |
---|
| 390 | assign s4_next = s0 ; |
---|
| 391 | assign s5_next = s1 ; |
---|
| 392 | |
---|
| 393 | always @ (posedge clk or negedge nrst) |
---|
| 394 | if (~nrst) |
---|
| 395 | begin |
---|
| 396 | s0 <= 1'b0 ; |
---|
| 397 | s1 <= 1'b0 ; |
---|
| 398 | s2 <= 1'b0 ; |
---|
| 399 | s3 <= 1'b0 ; |
---|
| 400 | s4 <= 1'b0 ; |
---|
| 401 | s5 <= 1'b0 ; |
---|
| 402 | end |
---|
| 403 | else if (ce) |
---|
| 404 | begin |
---|
| 405 | if (cc_start) |
---|
| 406 | begin |
---|
| 407 | s0 <= 1'b0 ; |
---|
| 408 | s1 <= 1'b0 ; |
---|
| 409 | s2 <= 1'b0 ; |
---|
| 410 | s3 <= 1'b0 ; |
---|
| 411 | s4 <= 1'b0 ; |
---|
| 412 | s5 <= 1'b0 ; |
---|
| 413 | end |
---|
| 414 | else if (vin) |
---|
| 415 | begin |
---|
| 416 | s0 <= s0_next ; |
---|
| 417 | s1 <= s1_next ; |
---|
| 418 | s2 <= s2_next ; |
---|
| 419 | s3 <= s3_next ; |
---|
| 420 | s4 <= s4_next ; |
---|
| 421 | s5 <= s5_next ; |
---|
| 422 | end |
---|
| 423 | end |
---|
| 424 | |
---|
| 425 | always @ (posedge clk or negedge nrst) |
---|
| 426 | if (~nrst) |
---|
| 427 | cnt <= 0 ; |
---|
| 428 | else if (ce) |
---|
| 429 | begin |
---|
| 430 | if (cc_start) |
---|
| 431 | cnt <= 0 ; |
---|
| 432 | else if (vin) |
---|
| 433 | begin |
---|
| 434 | if (cnt == 2) |
---|
| 435 | cnt <= 0 ; |
---|
| 436 | else |
---|
| 437 | cnt <= cnt +1 ; |
---|
| 438 | end |
---|
| 439 | end |
---|
| 440 | |
---|
| 441 | always @* |
---|
| 442 | begin |
---|
| 443 | mask = 8'd0 ; |
---|
| 444 | if (rate == 0) |
---|
| 445 | mask = 8'b1111_1111 ; |
---|
| 446 | else if (rate == 1) |
---|
| 447 | mask = 8'b1110_1110 ; |
---|
| 448 | else |
---|
| 449 | begin |
---|
| 450 | case(cnt) |
---|
| 451 | 0: mask = 8'b1110_0111 ; |
---|
| 452 | 1: mask = 8'b1001_1110 ; |
---|
| 453 | 2: mask = 8'b0111_1001 ; |
---|
| 454 | default: mask = 8'b1110_0111 ; |
---|
| 455 | endcase |
---|
| 456 | end |
---|
| 457 | end |
---|
| 458 | |
---|
| 459 | endmodule |
---|
| 460 | |
---|
| 461 | |
---|
| 462 | //************************************************************** |
---|
| 463 | // File: cc_puncture.v |
---|
| 464 | // Author: Yang Sun (ysun@rice.edu) |
---|
| 465 | // Created: $ 09/27/08 |
---|
| 466 | // Des: data puncture |
---|
| 467 | // |
---|
| 468 | // History: $ 09/27/08, created |
---|
| 469 | //************************************************************** |
---|
| 470 | module cc_puncture ( |
---|
| 471 | din , // I, data in |
---|
| 472 | mask , // I, data in mask |
---|
| 473 | dout , // O, data out |
---|
| 474 | ndout // O, number of output bits |
---|
| 475 | ) ; |
---|
| 476 | |
---|
| 477 | input [7:0] din ; |
---|
| 478 | input [7:0] mask ; |
---|
| 479 | |
---|
| 480 | output reg [7:0] dout ; |
---|
| 481 | output reg [3:0] ndout ; |
---|
| 482 | |
---|
| 483 | //=========================================== |
---|
| 484 | //Internal signal |
---|
| 485 | //=========================================== |
---|
| 486 | |
---|
| 487 | |
---|
| 488 | //=========================================== |
---|
| 489 | // Main RTL |
---|
| 490 | //=========================================== |
---|
| 491 | always @* |
---|
| 492 | begin |
---|
| 493 | ndout = 8 ; |
---|
| 494 | dout = din ; |
---|
| 495 | |
---|
| 496 | case (mask) |
---|
| 497 | 8'b1111_1111: begin // rate 1/2 |
---|
| 498 | ndout = 8 ; |
---|
| 499 | dout = din ; |
---|
| 500 | end |
---|
| 501 | 8'b1110_1110: begin // rate 2/3 |
---|
| 502 | ndout = 6 ; |
---|
| 503 | dout = {din[7:5], din[3:1], 1'b0, 1'b0} ; |
---|
| 504 | end |
---|
| 505 | 8'b1110_0111: begin // rate 3/4 |
---|
| 506 | ndout = 6 ; |
---|
| 507 | dout = {din[7:5], din[2:0], 1'b0, 1'b0} ; |
---|
| 508 | end |
---|
| 509 | 8'b1001_1110: begin // rate 3/4 |
---|
| 510 | ndout = 5 ; |
---|
| 511 | dout = {din[7], din[4:1], 1'b0, 1'b0, 1'b0} ; |
---|
| 512 | end |
---|
| 513 | 8'b0111_1001: begin // rate 3/4 |
---|
| 514 | ndout = 5 ; |
---|
| 515 | dout = {din[6:3], din[0], 1'b0, 1'b0, 1'b0} ; |
---|
| 516 | end |
---|
| 517 | endcase |
---|
| 518 | end |
---|
| 519 | |
---|
| 520 | endmodule |
---|
| 521 | |
---|
| 522 | //********************************************************* |
---|
| 523 | // File: my_fifo_a.v |
---|
| 524 | // Author: Y. Sun |
---|
| 525 | // Des: Asnyc read FIFO |
---|
| 526 | //********************************************************* |
---|
| 527 | module fifo_async_rd ( |
---|
| 528 | clk , // I, clock |
---|
| 529 | ce , // I, clock en |
---|
| 530 | nrst , // I, async reset |
---|
| 531 | reset , // I, sync reset |
---|
| 532 | wdata , // I, write data |
---|
| 533 | wr , // I, write enable |
---|
| 534 | rd , // I, read enable |
---|
| 535 | rdata , // I, read data |
---|
| 536 | empty , // O, empty |
---|
| 537 | half_full , // O, half full |
---|
| 538 | full // O, full |
---|
| 539 | ) ; |
---|
| 540 | |
---|
| 541 | parameter WIDTH = 8 ; |
---|
| 542 | parameter DEPTH = 8 ; |
---|
| 543 | parameter ADDR_BITS = 3 ; |
---|
| 544 | |
---|
| 545 | input clk ; |
---|
| 546 | input ce ; |
---|
| 547 | input nrst ; |
---|
| 548 | input reset ; |
---|
| 549 | input [WIDTH-1:0] wdata ; |
---|
| 550 | input wr ; |
---|
| 551 | input rd ; |
---|
| 552 | output [WIDTH-1:0] rdata ; |
---|
| 553 | output empty ; |
---|
| 554 | output half_full ; |
---|
| 555 | output full ; |
---|
| 556 | |
---|
| 557 | |
---|
| 558 | //============================ |
---|
| 559 | // Internal signals |
---|
| 560 | //============================ |
---|
| 561 | reg [WIDTH-1:0] mem [DEPTH-1:0] ; |
---|
| 562 | reg [ADDR_BITS:0] cnt ; |
---|
| 563 | reg [ADDR_BITS-1:0] waddr ; |
---|
| 564 | reg [ADDR_BITS-1:0] raddr ; |
---|
| 565 | |
---|
| 566 | //============================ |
---|
| 567 | // Main RTL Code |
---|
| 568 | //============================ |
---|
| 569 | assign empty = cnt == 0 ; |
---|
| 570 | assign full = cnt == DEPTH ; |
---|
| 571 | assign half_full = cnt >= DEPTH/2 ; |
---|
| 572 | |
---|
| 573 | |
---|
| 574 | always @ (posedge clk or negedge nrst) |
---|
| 575 | if(~nrst) |
---|
| 576 | cnt <= 0 ; |
---|
| 577 | else if(ce) |
---|
| 578 | begin |
---|
| 579 | if (reset) |
---|
| 580 | cnt <= 0 ; |
---|
| 581 | else if (wr & ~rd) |
---|
| 582 | cnt <= cnt +1 ; |
---|
| 583 | else if (rd & ~wr) |
---|
| 584 | cnt <= cnt -1 ; |
---|
| 585 | end |
---|
| 586 | |
---|
| 587 | always @ (posedge clk or negedge nrst) |
---|
| 588 | if(~nrst) |
---|
| 589 | waddr <= 0 ; |
---|
| 590 | else if(ce) |
---|
| 591 | begin |
---|
| 592 | if (reset) |
---|
| 593 | waddr <= 0 ; |
---|
| 594 | else if (wr) |
---|
| 595 | waddr <= waddr == DEPTH -1 ? 0 : waddr +1 ; |
---|
| 596 | end |
---|
| 597 | |
---|
| 598 | always @ (posedge clk or negedge nrst) |
---|
| 599 | if(~nrst) |
---|
| 600 | raddr <= 0 ; |
---|
| 601 | else if(ce) |
---|
| 602 | begin |
---|
| 603 | if (reset) |
---|
| 604 | raddr <= 0 ; |
---|
| 605 | else if (rd) |
---|
| 606 | raddr <= raddr == DEPTH -1 ? 0 : raddr +1 ; |
---|
| 607 | end |
---|
| 608 | |
---|
| 609 | always @ (posedge clk) |
---|
| 610 | if(ce) |
---|
| 611 | begin |
---|
| 612 | if (wr) |
---|
| 613 | mem [waddr] <= wdata ; |
---|
| 614 | end |
---|
| 615 | |
---|
| 616 | assign rdata = mem [raddr] ; |
---|
| 617 | |
---|
| 618 | endmodule |
---|
| 619 | |
---|
| 620 | //******************************************************************************** |
---|
| 621 | // File: sfifo_8to4 |
---|
| 622 | // Author: Yang Sun (ysun@rice.edu) |
---|
| 623 | // Birth: $ 11/24/07 |
---|
| 624 | // Des: Small size serial fifo |
---|
| 625 | // History: $ 11/24/07, Created |
---|
| 626 | // $ 11/26/07, Modify input format |
---|
| 627 | //******************************************************************************** |
---|
| 628 | module sfifo_8to4 ( |
---|
| 629 | clk , // clock |
---|
| 630 | ce , // clock enable |
---|
| 631 | nrst , // asyn reset |
---|
| 632 | reset , // sync reset |
---|
| 633 | din , // data input |
---|
| 634 | dout , // data output |
---|
| 635 | rd , // read |
---|
| 636 | wr , // write |
---|
| 637 | full , // fifo full |
---|
| 638 | empty , // fifo empty |
---|
| 639 | nsin , // num of shift in (bits), could be 4, 6 or 8 |
---|
| 640 | nsout // num of shift out (bits), could be 2 or 4. |
---|
| 641 | ) ; |
---|
| 642 | |
---|
| 643 | input clk ; |
---|
| 644 | input ce ; |
---|
| 645 | input nrst ; |
---|
| 646 | input reset ; |
---|
| 647 | input [7:0] din ; |
---|
| 648 | output [3:0] dout ; |
---|
| 649 | input rd ; |
---|
| 650 | input wr ; |
---|
| 651 | output full ; |
---|
| 652 | output empty ; |
---|
| 653 | input [3:0] nsin ; |
---|
| 654 | input [3:0] nsout ; |
---|
| 655 | |
---|
| 656 | //========================= |
---|
| 657 | // Internal signal |
---|
| 658 | //========================= |
---|
| 659 | reg [15:0] buffer ; |
---|
| 660 | reg [4:0] cnt ; |
---|
| 661 | wire [19:0] shift_out ; |
---|
| 662 | wire [23:0] shift_in ; |
---|
| 663 | wire [4:0] space_left ; |
---|
| 664 | |
---|
| 665 | //========================= |
---|
| 666 | // Main RTL |
---|
| 667 | //========================= |
---|
| 668 | assign full = (cnt + nsin) > 16 ; |
---|
| 669 | assign empty = cnt < nsout ; |
---|
| 670 | |
---|
| 671 | always @ (posedge clk or negedge nrst) |
---|
| 672 | if (~nrst) |
---|
| 673 | cnt <= 0 ; |
---|
| 674 | else if (ce) |
---|
| 675 | begin |
---|
| 676 | if (reset) |
---|
| 677 | cnt <= 0 ; |
---|
| 678 | else |
---|
| 679 | begin |
---|
| 680 | if (wr & rd) |
---|
| 681 | cnt <= cnt +nsin -nsout ; |
---|
| 682 | if (wr & ~rd) |
---|
| 683 | cnt <= cnt +nsin ; |
---|
| 684 | if (~wr & rd) |
---|
| 685 | cnt <= cnt -nsout ; |
---|
| 686 | end |
---|
| 687 | end |
---|
| 688 | |
---|
| 689 | assign shift_out = {4'h0, buffer} << nsout ; |
---|
| 690 | assign dout = shift_out [19:16] ; |
---|
| 691 | |
---|
| 692 | assign space_left = rd ? 16 -cnt + nsout : 16 -cnt ; |
---|
| 693 | assign shift_in = {16'h0, din} << space_left ; |
---|
| 694 | |
---|
| 695 | always @ (posedge clk or negedge nrst) |
---|
| 696 | if (~nrst) |
---|
| 697 | buffer <= 0 ; |
---|
| 698 | else if (ce) |
---|
| 699 | begin |
---|
| 700 | if (reset) |
---|
| 701 | buffer <= 0 ; |
---|
| 702 | else |
---|
| 703 | begin |
---|
| 704 | if (wr & rd) |
---|
| 705 | buffer <= shift_out [15:0] | shift_in [23:8] ; |
---|
| 706 | if (wr & ~rd) |
---|
| 707 | buffer <= buffer | shift_in [23:8] ; |
---|
| 708 | if (~wr & rd) |
---|
| 709 | buffer <= shift_out [15:0] ; |
---|
| 710 | end |
---|
| 711 | end |
---|
| 712 | |
---|
| 713 | endmodule |
---|
| 714 | |
---|
| 715 | //******************************************************************************** |
---|
| 716 | // File: sfifo_8to8 |
---|
| 717 | // Author: Yang Sun (ysun@rice.edu) |
---|
| 718 | // Des: Small size serial fifo, n bit in, 8 bit out |
---|
| 719 | //******************************************************************************** |
---|
| 720 | module sfifo_nto8 ( |
---|
| 721 | clk , // clock |
---|
| 722 | ce , // clock enable |
---|
| 723 | nrst , // asyn reset |
---|
| 724 | reset , // sync reset |
---|
| 725 | din , // data input |
---|
| 726 | dout , // data output |
---|
| 727 | rd , // read |
---|
| 728 | wr , // write |
---|
| 729 | full , // fifo full |
---|
| 730 | empty , // fifo empty |
---|
| 731 | nsin , // num of shift in (bits) |
---|
| 732 | nsout // num of shift out (bits) |
---|
| 733 | ) ; |
---|
| 734 | |
---|
| 735 | input clk ; |
---|
| 736 | input ce ; |
---|
| 737 | input nrst ; |
---|
| 738 | input reset ; |
---|
| 739 | input [7:0] din ; |
---|
| 740 | output [7:0] dout ; |
---|
| 741 | input rd ; |
---|
| 742 | input wr ; |
---|
| 743 | output full ; |
---|
| 744 | output empty ; |
---|
| 745 | input [3:0] nsin ; |
---|
| 746 | input [3:0] nsout ; |
---|
| 747 | |
---|
| 748 | //========================================= |
---|
| 749 | // Internal signal |
---|
| 750 | //========================================= |
---|
| 751 | reg [23:0] buffer ; // 24-bit buffer |
---|
| 752 | reg [5:0] cnt ; // counter to record num of bits loaded into buffer |
---|
| 753 | wire [31:0] shift_out ; |
---|
| 754 | wire [31:0] shift_in ; |
---|
| 755 | wire [5:0] space_left ; |
---|
| 756 | |
---|
| 757 | //========================================= |
---|
| 758 | // Main RTL |
---|
| 759 | //========================================= |
---|
| 760 | assign dout = shift_out [31:24] ; |
---|
| 761 | assign full = (cnt + nsin) > 24 ; |
---|
| 762 | assign empty = cnt < nsout ; |
---|
| 763 | |
---|
| 764 | always @ (posedge clk or negedge nrst) |
---|
| 765 | if (~nrst) |
---|
| 766 | cnt <= 0 ; |
---|
| 767 | else if (ce) |
---|
| 768 | begin |
---|
| 769 | if (reset) |
---|
| 770 | cnt <= 0 ; |
---|
| 771 | else |
---|
| 772 | begin |
---|
| 773 | if (wr & rd) |
---|
| 774 | cnt <= cnt + nsin - nsout ; |
---|
| 775 | if (wr & ~rd) |
---|
| 776 | cnt <= cnt + nsin ; |
---|
| 777 | if (~wr & rd) |
---|
| 778 | cnt <= cnt - nsout ; |
---|
| 779 | end |
---|
| 780 | end |
---|
| 781 | |
---|
| 782 | assign shift_out = {8'h0, buffer} << nsout ; |
---|
| 783 | assign space_left = rd ? 24 -cnt + nsout : 24 -cnt ; |
---|
| 784 | assign shift_in = {24'h0, din} << space_left ; |
---|
| 785 | |
---|
| 786 | always @ (posedge clk or negedge nrst) |
---|
| 787 | if (~nrst) |
---|
| 788 | buffer <= 0 ; |
---|
| 789 | else if (ce) |
---|
| 790 | begin |
---|
| 791 | if (reset) |
---|
| 792 | buffer <= 0 ; |
---|
| 793 | else |
---|
| 794 | begin |
---|
| 795 | if (wr & rd) |
---|
| 796 | buffer <= shift_out [23:0] | shift_in [31:8] ; |
---|
| 797 | if (wr & ~rd) |
---|
| 798 | buffer <= buffer | shift_in [31:8] ; |
---|
| 799 | if (~wr & rd) |
---|
| 800 | buffer <= shift_out [23:0] ; |
---|
| 801 | end |
---|
| 802 | end |
---|
| 803 | |
---|
| 804 | endmodule |
---|