Advertisement
Guest User

bin2bcd

a guest
Jun 3rd, 2016
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Listing 6.6
  2. module bin2bcd
  3.    (
  4.     input wire clk, reset,
  5.     input wire start,
  6.     input wire [13:0] bin,
  7.     output reg ready, done_tick,
  8.     output wire [3:0] bcd3, bcd2, bcd1, bcd0
  9.    );
  10.  
  11.    // symbolic state declaration
  12.    localparam [1:0]
  13.       idle = 2'b00,
  14.       op   = 2'b01,
  15.       done = 2'b10;
  16.  
  17.    // signal declaration
  18.    reg [1:0] state_reg, state_next;
  19.    reg [13:0] p2s_reg, p2s_next;
  20.    reg [3:0] n_reg, n_next;
  21.    reg [3:0] bcd3_reg, bcd2_reg, bcd1_reg, bcd0_reg;
  22.    reg [3:0] bcd3_next, bcd2_next, bcd1_next, bcd0_next;
  23.    wire [3:0] bcd3_tmp, bcd2_tmp, bcd1_tmp, bcd0_tmp;
  24.  
  25.  
  26.    // body
  27.    // FSMD state & data registers
  28.    always @(posedge clk, posedge reset)
  29.       if (reset)
  30.          begin
  31.             state_reg <= idle;
  32.             p2s_reg <= 0;
  33.             n_reg <= 0;
  34.             bcd3_reg <= 0;
  35.             bcd2_reg <= 0;
  36.             bcd1_reg <= 0;
  37.             bcd0_reg <= 0;
  38.          end
  39.       else
  40.          begin
  41.             state_reg <= state_next;
  42.             p2s_reg <= p2s_next;
  43.             n_reg <= n_next;
  44.             bcd3_reg <= bcd3_next;
  45.             bcd2_reg <= bcd2_next;
  46.             bcd1_reg <= bcd1_next;
  47.             bcd0_reg <= bcd0_next;
  48.          end
  49.  
  50.  
  51.    // FSMD next-state logic
  52.    always @*
  53.    begin
  54.       state_next = state_reg;
  55.       ready = 1'b0;
  56.       done_tick = 1'b0;
  57.       p2s_next = p2s_reg;
  58.       bcd0_next = bcd0_reg;
  59.       bcd1_next = bcd1_reg;
  60.       bcd2_next = bcd2_reg;
  61.       bcd3_next = bcd3_reg;
  62.       n_next = n_reg;
  63.       case (state_reg)
  64.          idle:
  65.             begin
  66.                ready = 1'b1;
  67.                if (start)
  68.                   begin
  69.                      state_next = op;
  70.                      bcd3_next = 0;
  71.                      bcd2_next = 0;
  72.                      bcd1_next = 0;
  73.                      bcd0_next = 0;
  74.                      n_next = 4'b1110; // index
  75.                      p2s_next = bin;   // shift register
  76.                      state_next = op;
  77.                   end
  78.             end
  79.          op:
  80.             begin
  81.                // shift in binary bit
  82.                p2s_next = p2s_reg << 1;
  83.                // shift 4 BCD digits
  84.                //{bcd3_next, bcd2_next, bcd1_next, bcd0_next}=
  85.                //{bcd3_tmp[2:0], bcd2_tmp, bcd1_tmp, bcd0_tmp,
  86.                // p2s_reg[12]}
  87.  
  88.                bcd0_next = {bcd0_tmp[2:0], p2s_reg[13]};
  89.                bcd1_next = {bcd1_tmp[2:0], bcd0_tmp[3]};
  90.                bcd2_next = {bcd2_tmp[2:0], bcd1_tmp[3]};
  91.                bcd3_next = {bcd3_tmp[2:0], bcd2_tmp[3]};
  92.                n_next = n_reg - 1;
  93.                if (n_next==0)
  94.                    state_next = done;
  95.             end
  96.          done:
  97.             begin
  98.                done_tick = 1'b1;
  99.                state_next = idle;
  100.             end
  101.          default: state_next = idle;
  102.       endcase
  103.    end
  104.  
  105.    // data path function units
  106.    assign bcd0_tmp = (bcd0_reg > 4) ? bcd0_reg+3 : bcd0_reg;
  107.    assign bcd1_tmp = (bcd1_reg > 4) ? bcd1_reg+3 : bcd1_reg;
  108.    assign bcd2_tmp = (bcd2_reg > 4) ? bcd2_reg+3 : bcd2_reg;
  109.    assign bcd3_tmp = (bcd3_reg > 4) ? bcd3_reg+3 : bcd3_reg;
  110.  
  111.    // output
  112.    assign bcd0 = bcd0_reg;
  113.    assign bcd1 = bcd1_reg;
  114.    assign bcd2 = bcd2_reg;
  115.    assign bcd3 = bcd3_reg;
  116.  
  117. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement