Advertisement
Guest User

Untitled

a guest
Oct 31st, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. `define SW 3
  2. `define WAIT    3'b000
  3. `define DECODE  3'b001
  4. `define ALU_CLR 3'b010
  5. `define ALU_ADD 3'b011
  6. `define ALU_SH  3'b100
  7. `define GET_A   3'b101
  8. `define DONE    3'b110
  9.  
  10. module calc(clk, reset, s, in, op, out, ovf, done);
  11.  input clk, reset, s;
  12.  input [7:0] in;
  13.  input [1:0] op;
  14.  output [7:0] out;
  15.  output ovf, done;
  16.  
  17.  reg done;
  18.  
  19.  reg selA, loadA, loadB, loadV;
  20.  wire [7:0] ALUout, outB, outC;
  21.  
  22.  wire outV;
  23.  wire zero;
  24.  
  25.  wire [2:0] presentState, nextStateReset;
  26.  reg  [2:0] nextState;
  27.  
  28.  counter C(clk, selA, loadA, in, outC);
  29.  ALU #(8) alu(outC, op, outB, ALUout, outV);
  30.  
  31.  RegLoad #(8) B(clk, loadB, ALUout, outB);
  32.  RegLoad #(1) V(clk, loadV, outV, ovf);
  33.  
  34.  assign out = outB;
  35.  assign zero = ~outC[0] & ~outC[1] & ~outC[2] & ~outC[3] & ~outC[4] & ~outC[5] & ~outC[6] & ~outC[7];
  36.  
  37.  vDFF #(`SW) STATE(clk, nextStateReset, presentState);
  38.  assign nextStateReset = reset ? `WAIT : nextState;
  39.  
  40.  always @(*) begin
  41.     case(presentState)
  42.         `WAIT       : begin
  43.                         done = 0;
  44.                         loadB = 0;
  45.                         loadV = 0;
  46.                         selA = 1;
  47.                         loadA = 0;
  48.                         nextState = s ? `DECODE : `WAIT;
  49.                        
  50.                         end
  51.         `DECODE     : begin
  52.                         done = 0;
  53.                         loadB = 0;
  54.                         loadV = 0;
  55.                         selA = 1;
  56.                         loadA = 0;
  57.                         case(op)
  58.                             2'b00 : nextState = `ALU_CLR;
  59.                             2'b01 : nextState = `GET_A;
  60.                             2'b10 : nextState = `GET_A;
  61.                             default : nextState = 3'bxxx;
  62.                         endcase
  63.                     end
  64.         `ALU_CLR    : begin
  65.                         loadB = 1;
  66.                         done = 0;
  67.                         selA = 1;
  68.                         loadV = 0;
  69.                         loadA = 0;
  70.                         nextState = `DONE;
  71.                     end
  72.         `ALU_ADD    : begin
  73.                         loadB = 1;
  74.                         loadV = 1;
  75.                         done = 0;
  76.                         selA = 1;
  77.                         loadA = 0;
  78.                         nextState = `DONE;
  79.                     end
  80.         `ALU_SH     : begin
  81.                         loadB = 1;
  82.                         done = 0;
  83.                         selA = 1;
  84.                         loadV = 0;
  85.                         loadA = 0;
  86.                         nextState = `DONE;
  87.                     end
  88.         `GET_A      : begin
  89.                         selA = 1;
  90.                         loadA = 1;
  91.                         loadB = 0;
  92.                         loadV = 0;
  93.                         done = 0;
  94.                        
  95.                         case(op)
  96.                             2'b01 : nextState = `ALU_ADD;
  97.                             2'b10 : nextState = `ALU_SH;
  98.                             default : nextState = 3'bxxx;
  99.                         endcase
  100.                     end
  101.         `DONE       : begin
  102.                         loadB = 0;
  103.                         done = 1;
  104.                         selA = 1;
  105.                         loadV = 0;
  106.                         loadA = 0;
  107.                         nextState = `WAIT;
  108.                     end
  109.         default : begin
  110.                     done = 0;
  111.                     loadB = 0;
  112.                     loadV = 0;
  113.                     selA = 1;
  114.                     loadA = 0;
  115.                     nextState = 3'bxxx;
  116.                 end
  117.     endcase
  118.  end
  119.  
  120.  
  121.  
  122. endmodule
  123.  
  124.  
  125. module RegLoad( clk, load, in, out) ;
  126.   parameter n = 1;  // width
  127.   input load, clk;
  128.   input     [n-1:0] in ;
  129.   output    [n-1:0] out;
  130.   reg       [n-1:0] out;
  131.   wire      [n-1:0] next;
  132.  
  133.   // Flip flop, assigns output on rising edge of clk
  134.   always @(posedge clk)
  135.     out = next ;
  136.   // Multiplexer using ? operator, assigns next to in if load is 1, and out if load is 0
  137.   assign next = load ? in : out;
  138. endmodule // RegLoad
  139.  
  140. module counter(clk, selA, loadA, in, outC);
  141.     input clk, selA, loadA;
  142.     input [7:0] in;
  143.     output [7:0] outC;
  144.    
  145.     wire [7:0] muxOut;
  146.    
  147.    
  148.     RegLoad #(8) A(clk, loadA, muxOut, outC);
  149.    
  150.     assign muxOut = selA ? in : outC - 8'b0000_0001;
  151.    
  152. endmodule
  153.  
  154. module ALU(in, op, outB, ALUout, outV);
  155.     parameter n = 8;
  156.     input [n-1:0] in;
  157.     input [1:0] op;
  158.     input [n-1:0] outB;
  159.     output reg [n-1:0] ALUout;
  160.     output outV;
  161.    
  162.     wire [7:0] addedR;
  163.    
  164.     AddSub add(outB, in, 1'b0, addedR, outV);
  165.    
  166.     always @(*) begin
  167.         case (op)
  168.             2'b00 : ALUout = 0;  // CLEAR
  169.             2'b01 : ALUout = addedR;
  170.             2'b10 : begin
  171.                         ALUout = outB >>> in;
  172.                     end
  173.         endcase
  174.     end
  175.    
  176. endmodule
  177.  
  178. // from slide set 6
  179. // add a+b or subtract a-b, check for overflow
  180. module AddSub(a,b,sub,s,ovf) ;
  181.   parameter n = 8 ;
  182.   input [n-1:0] a, b ;
  183.   input sub ;           // subtract if sub=1, otherwise add
  184.   output [n-1:0] s ;
  185.   output ovf ;          // 1 if overflow
  186.   wire c1, c2 ;         // carry out of last two bits
  187.   wire ovf = c1 ^ c2 ;  // overflow if signs don't match
  188.  
  189.   // add non sign bits
  190.   Adder1 #(n-1) ai(a[n-2:0],b[n-2:0]^{n-1{sub}},sub,c1,s[n-2:0]) ;
  191.   // add sign bits
  192.   Adder1 #(1)   as(a[n-1],b[n-1]^sub,c1,c2,s[n-1]) ;
  193. endmodule
  194.  
  195. // from slide set 6
  196. // multi-bit adder - behavioral
  197. module Adder1(a,b,cin,cout,s) ;
  198.   parameter n = 8 ;
  199.   input [n-1:0] a, b ;
  200.   input cin ;
  201.   output [n-1:0] s ;
  202.   output cout ;
  203.   wire [n-1:0] s;
  204.   wire cout ;
  205.  
  206.   assign {cout, s} = a + b + cin ;
  207. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement