Friday 22 April 2016

Microprocessor 8085 Verilog


  1. /*module processor(a,b, );
  2.   input [2:0]opcode;
  3.   input a,b;
  4.   output o;

  5. case(opcode)
  6.   begin
  7.   000: assign o<=a+b;
  8.   001: assign o<a-b;
  9.   010: assign 
  10.   */
  11.   
  12. module meomory(addr,,enable,out);
  13. input [2:0]addr;    //pc
  14. output reg [10:0] out;
  15. reg [10:0]mem[7:0];
  16. input enable;      //read
  17. initial
  18. $readmemb("input.txt",mem);
  19.   always @(*)
  20.   if(enable)
  21.  out=mem[addr];
  22.  else
  23.    out=out;
  24.    
  25. endmodule


  26. module ir(a,en,out);
  27.   input [10:0]a;
  28.   input en;
  29.   output reg [10:0]out;
  30.   always @(*)
  31.   begin
  32.     if(en==1)
  33.   out=a;
  34. end
  35. endmodule

  36. module decoder(in,en,opcode,data);
  37. input [10:0]in;
  38. input en;
  39. output reg [2:0]opcode; 
  40. output reg[7:0]data;
  41.   always @(*)
  42.   begin
  43.     if(en==1)
  44.       begin
  45.   opcode=in[10:8];
  46.   data=in[7:0];
  47.    end
  48.    /*else
  49.      begin
  50.      opcode=0;
  51.      data=0;
  52.     end*/
  53. end
  54. endmodule

  55. //module meomory_tb;
  56. //  reg [3:0]addr;
  57. //  reg enable;
  58.   
  59. module ir_tb;
  60.   reg [10:0]a;
  61.   reg en;
  62.   wire [10:0]out;
  63.   ir v1(a,en,out);
  64.   
  65.   initial
  66.     begin 
  67.       #1 en=1;
  68.       #5 a=11'b10001100110;
  69.       #10 en=0;
  70.       #11 a=11'b01011001101;
  71.   end
  72. endmodule

  73. module decoder_tb;
  74.   reg [10:0]in;
  75. reg en;
  76. wire [2:0]opcode; 
  77. wire [7:0]data;
  78. decoder v2(in,en,opcode,data);
  79.    initial
  80.      begin
  81.        #1 en=1;
  82.       #5 in=11'b10001100110;
  83.       #10 en=0;
  84.       #11 in=11'b01011001101;
  85.   end
  86. endmodule

  87. module alu4pro(opcode,vala,valb,aluout);
  88.   input [2:0]opcode;
  89.   input [7:0]vala;
  90.   input [7:0]valb;
  91.   output reg [10:0]aluout;
  92.   parameter add_op=3'b000,sub_op=3'b001,nand_op=3'b010,nor_op=3'b011,xor_op=3'b100,and_op=3'b101,or_op=3'b110,xnor_op=3'b111;

  93.   always @(*)
  94.   
  95.   case(opcode)
  96.   
  97.       
  98.     add_op: begin
  99.       aluout=vala+valb;
  100.     end
  101.     
  102.     sub_op:begin
  103.       aluout=vala-valb;
  104.     end
  105.     
  106.     nand_op:begin
  107.       aluout=~(vala&valb);
  108.     end
  109.     
  110.     nor_op:begin
  111.       aluout=~(vala|valb);
  112.     end
  113.     
  114.     xor_op:begin
  115.       aluout=vala^valb;
  116.     end
  117.     
  118.     and_op:begin
  119.       aluout=vala&valb;
  120.     end
  121.     
  122.     or_op:begin
  123.       aluout=vala|valb;
  124.     end
  125.     
  126.     xnor_op:begin
  127.       aluout=~(vala^valb);
  128.     end
  129.   
  130.   endcase
  131.     
  132. endmodule

  133. module alu4pro_tb;
  134.   reg [2:0]opcode;
  135.   reg [7:0]vala;
  136.   reg [7:0]valb;
  137.   wire [10:0]aluout;
  138.   alu4pro b2(opcode,vala,valb,aluout);
  139.   initial
  140.   begin
  141.     opcode=3'b000;
  142.     vala=3'b10;
  143.     valb=3'b11;
  144. #20opcode=3'b110;
  145. vala=3'b111;
  146. valb=3'b101;
  147. end
  148. //$monitor("time=%d,opcode=%b,vala=%b,valb=%b,aluout=%b",$time,opcode,vala,valb,aluout);
  149. endmodule

  150. module accumulator(aluout,clk,rst,accout);
  151.   input [7:0]aluout;
  152.   input clk;
  153.   input rst;
  154.   output reg [7:0]accout;
  155.   always@(clk)
  156.   if(rst)
  157.     begin
  158.      accout=aluout;
  159.   end
  160. else
  161.   accout=0;
  162. endmodule

  163. module accumulator_tb;
  164.   reg [7:0]aluout;
  165.   reg clk;
  166.   reg rst;
  167.   wire [7:0]accout;
  168.   accumulator b3(aluout,clk,rst,accout);
  169.   initial
  170.     begin
  171.       clk=0;
  172.       rst=1;
  173.       aluout=6'b101011;
  174.       #10 rst=0;
  175.       #15 rst=1;
  176.       
  177.     end
  178.       always #5 clk=~clk;
  179. endmodule
  180.  
  181. /*module controller();
  182.   output reg rd,rw,de,en;
  183.   reg ps,ns;
  184.  input clk,rst;
  185.   always @(clk)
  186. */
  187. module controller(rst,clk,read,addr,fe,de,ex);
  188.   input rst,clk;
  189.   reg [2:0]ps;
  190.   reg [2:0]ns;
  191.   output reg read,fe,de,ex;
  192.   parameter fetch=3'b000,decode=3'b001,execute=3'b010;
  193.   output reg [3:0]addr=4'b0000;
  194.   always @(posedge clk ,posedge rst)
  195.     if (rst)
  196.       ps<=fetch;
  197.     else
  198.       ps<=ns;
  199.     
  200. always @(clk)
  201.    case(ps)
  202.      fetch: begin
  203.        read=1;
  204.        fe=0;
  205.        de=0;
  206.        ex=0;
  207.     end
  208.     decode:begin
  209.       read=0;
  210.        fe=0;
  211.        de=1;
  212.        ex=0;
  213.      end
  214.      execute:begin
  215.       read=0;
  216.        addr=addr+1;
  217.        fe=0;
  218.        de=0;
  219.        ex=1;
  220.      end
  221. endcase
  222. endmodule


  223. module controller_tb;
  224.   reg rst,clk;
  225.   //reg[2:0]ns;
  226.   wire read;
  227.   wire fe,de,ex;
  228.   controller b1(rst,clk,read,fe,de,ex);
  229.   initial
  230.    begin
  231.      clk=0;
  232.      rst=0;
  233.      #10 rst=1;
  234.      #12 rst=0;
  235.     // #2 ns=3'b001;
  236.   end
  237.      always #5 clk=~clk;
  238. endmodule


  239. module processor(rst,clk,inp,outp);
  240.   input rst;
  241.   input clk;
  242.   input [10:0]inp;
  243.   output [10:0]outp;
  244.   wire accout;
  245.   wire rd;
  246.   wire ex;
  247.   wire de;
  248.   wire read;
  249.   wire addr;  //pc
  250.   wire mout; //meomory output
  251.   wire opcode;
  252.   meomory hk1(addr,read,mout); //read=enable
  253.   ir hk2(mout,rd,out);  //rd-enable  mout-a
  254.   decoder hk3(out,de,opcode,data); //de-enable
  255.   alu4pro hk4(opcode,inp,accout,aluout);    //decoder ka de 8bit data kaise use karon !!!! pehle hi ek acc se i/p ek main i/p donga is module se dusra i/p decoder wale ka kya karonga ...refer to diagram 
  256.   accumulator hk5(outp,clk,ex,accout);  //ex-rst
  257.   controller hk6(rst,clk,read,addr,fe,de,ex);
  258.   
  259. endmodule

  260. module processor_tb;
  261.   reg rst,clk;
  262.   reg[7:0]inp;
  263.   wire [7:0]outp;
  264.   processor hk10(rst,clk,inp,outp);
  265.   initial
  266.     begin
  267.       rst=0;
  268.       clk=0;
  269.       #2 inp=8'b01110101;
  270.       #5 rst=1;
  271.       #7 rst=0;
  272.     end
  273.     
  274.   always #10 clk=~clk;
  275. endmodule
simulation of proccessor_tb

Simulation of ir_tb



Simulation of decoder_tb


simulation of alu4pro_tb


simulation of controller_tb



simulation of accumulator_tb


No comments:

Post a Comment