Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module program_counter(pc, clk, control, offset);
  2.     input clk;
  3.     input control;
  4.     input [15:0] offset;
  5.     reg extended_offset = 32'b0;
  6.     output reg [31:0] pc = 0;
  7.     always@(posedge clk)
  8.         begin
  9.             extended_offset = $signed(offset);
  10.             case(control)
  11.                 0: pc = pc + 4;
  12.                 1: pc = pc + (extended_offset*4)+4;
  13.             endcase
  14.         end
  15. endmodule
  16.  
  17. module data_memory(data_out, mem_write, mem_read, address, data_in);
  18.     output [31:0] data_out;
  19.     input mem_write, mem_read;
  20.     input [31:0] address, data_in;
  21.     reg [255:0] memory [31:0];
  22.    
  23.     assign data_out = mem_read ? (memory[address]):32'bx;
  24.     always@(mem_write)
  25.         begin
  26.             memory[address] = data_in;
  27.         end
  28. endmodule
  29.  
  30. module mux_2x1_32b(result,i1,i2,select);
  31.     output reg [31:0] result ;
  32.     input [31:0] i1;
  33.     input [31:0] i2;
  34.     input select ;
  35.     always@(i1, i2, select)
  36.         result = (select)?i1:i2;
  37. endmodule
  38.  
  39. module instruction_memory(instruction, read_address);
  40.     parameter N = 255;
  41.     output [31:0] instruction;
  42.     input [31:0] read_address;
  43.     reg [31:0] memory [N:0];
  44.     initial
  45.     begin
  46.         $readmemb("instructions.txt", memory);
  47.     end
  48.     assign instruction = memory[read_address];
  49. endmodule
  50.  
  51. module if_stage(pc, instruction, pcSrc, offset, clk);
  52.     output reg [31:0] pc = 0;
  53.     output [31:0] instruction;
  54.  
  55.     wire [31:0] _pc;
  56.     input pcSrc, clk;
  57.     input [31:0] offset;
  58.    
  59.     assign _pc = pc;
  60.     assign instruction = 0;
  61.    
  62.     always@(posedge clk)
  63.         pc = pcSrc?(pc+(offset*4)):(pc+1);
  64.    
  65. endmodule
  66.  
  67. module main;
  68.     reg clk = 1'b0;
  69.     reg pcSrc = 1'b0;
  70.     wire [31:0] pc = 0;
  71.     wire [31:0] instruction = 0;
  72.     reg [31:0] offset = 0;
  73.     if_stage if_stage(pc, instruction, pcSrc, offset, clk);
  74.    
  75.     initial #300 $finish;
  76.     initial
  77.         begin
  78.             repeat (10)
  79.             #10 clk = ~clk ;
  80.         end
  81.    
  82.     initial
  83.     begin
  84.         $monitor("Clk:%d, PC:%d, I:%d, PCSrc:%d, Offset:%d", clk, pc, instruction, pcSrc, offset);
  85.     end
  86.    
  87. endmodule
  88.  
  89. /*
  90. module main;
  91.     reg clk = 1'b0;
  92.     reg control = 1'b0;
  93.     reg [15:0] offset = 0;
  94.     wire [31:0] pc;
  95.    
  96.     initial #300 $finish;
  97.     initial
  98.         begin
  99.             repeat (10)
  100.             #10 clk = ~clk ;
  101.         end
  102.     program_counter program(pc, clk, control, offset);
  103.     always@(posedge clk)
  104.         begin
  105.             $display("Clock: %d", clk);
  106.             $display("PC: %d", pc);
  107.             $display("Control: %d", control);
  108.             $display("Offset: %d", offset);
  109.             $display("-------------");
  110.         end
  111. endmodule */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement