Sunday, November 11, 2012
Tuesday, June 12, 2012
Testbench environment in SV for Round-robin arbiter for 4 masters
Here Testing Environment is created for this given arbiter DUT. DUT code is created such a way that logic inference take minimum area. Environment is created using SV and simulation is done using Modelsim SE PLUS 6.4.
//DUT top module
module arbiter(input clk, rst,input ack, input[3:0] req,
output reg [3:0] grant,output [3:0] token_ring);
reg [1:0]qd=0;
reg [1:0]c=0;
reg [3:0]q=4'b0001;
wire[3:0]en;
wire [3:0]in0;
wire [3:0]out1;
wire [3:0]out0;
wire [3:0]out2;
wire [3:0]out3;
//4 Priority encoder instantiation
priority_logic1 uut0(.in(in0),.en(en[0]),.out(out0));
priority_logic1 uut1(.in(in0),.en(en[1]),.out(out1));
priority_logic1 uut2(.in(in0),.en(en[2]),.out(out2));
priority_logic1 uut3(.in(in0),.en(en[3]),.out(out3));
//Shift_1 modue instant
shift_1 uutt0(req,in0,c);
assign en=q;
assign token_ring=q;
always @(out1 or out2 or out3 or out0)//(posedge arb.clk)
begin
if(rst) //active low reset
begin
grant[0]<=out0[0]|out1[3]|out2[2]|out3[1];
grant[1]<=out0[1]|out1[0]|out2[3]|out3[2];
grant[2]<=out0[2]|out1[1]|out2[0]|out3[3];
grant[3]<=out0[3]|out1[2]|out2[1]|out3[0];
end
else
begin
grant<=4'b0000;
end
//$strobe($time," :DUT : request = %b, ack = %b , grant = %b, reset = %b",req,ack,grant,rst);
end
//To implement ring counter functionality
always @(posedge ack)
begin
if (rst)
if(q<8)
begin
q<=2*q;
c<=c+1; //count till c=3
end
else
begin
c<=0;
q<=4'b0001;
end
else
begin
c<=0;
q<=4'b0001;
end
end
endmodule
//Priority encoder module
module priority_logic1(
input [3:0]in,
input en,
output [3:0]out
);
assign out=en?(in[0]?4'b0001:(in[1]?4'b0010:(in[2]?4'b0100:(in[3]?4'b1000:4'b0000)))):4'b0000;
endmodule
//Shift_1 module to implement round-robin priority functionality
module shift_1(
input [3:0]datain,
output [3:0]dataout,
input [1:0]sel);
wire [3:0]d0;
wire [3:0]d1;
assign dataout=d1;
assign d0=sel[0]?{datain[0],datain[3:1]}:datain;
assign d1=sel[1]?{d0[1:0],d0[3:2]}:d0;
endmodule
Reference model code :
//reference model
module reference_model(
input clk,
input rst,
input [3:0] req,
// output ack,
input ack,
output reg [3:0] gnt
);
reg [3:0] ringout;
//ack_generator ack1(.clk(clk),.rst(rst),.ack(ack));
always@(posedge ack or negedge rst)
//always@(posedge clk)
begin
if(~rst) //active low reset, ring count=4'b0001
ringout <= 4'b0001;
else if(ack) //ring counter will be changed only when acknowledge will assert
ringout <= {ringout[2:0],ringout[3]};
else
ringout <= ringout; //elsering counter will not change state
end
//always@(posedge ack)
always@(req or ringout) //grant will be changed based on ring counter state or request change
if ( ringout[0]) //if ringout=4'b0001 then 1>2>3>4
begin
casex (req)
4'bxxx1: gnt=4'b0001;
4'bxx10: gnt=4'b0010;
4'bx100: gnt=4'b0100;
4'b1000: gnt=4'b1000;
default: gnt=4'b0000;
endcase
$display($time," :Ref_model : request = %b, ack = %b , grant = %b, reset = %b, token = %b",req[3:0],ack,gnt[3:0],rst,ringout[3:0]);
end
else if(ringout[1]) //if ringout=4'b0010 then 2>3>4>1
begin
casex (req)
4'bxx1x: gnt=4'b0010;
4'bx10x: gnt=4'b0100;
4'b100x: gnt=4'b1000;
4'b0001: gnt=4'b0001;
default: gnt=4'b0000;
endcase
$display($time," :Ref_model : request = %b, ack = %b , grant = %b, reset = %b, token = %b",req[3:0],ack,gnt[3:0],rst,ringout[3:0]);
end
else if( ringout[2]) //if ringout=4'b0100 then 3>4>1>2
begin
casex (req)
4'bx1xx: gnt=4'b0100;
4'b10xx: gnt=4'b1000;
4'b00x1: gnt=4'b0001;
4'b0010: gnt=4'b0010;
default: gnt=4'b0000;
endcase
$display($time," :Ref_model : request = %b, ack = %b , grant= %b, reset = %b, token = %b",req[3:0],ack,gnt[3:0],rst,ringout[3:0]);
end
else
begin
casex (req) //if ringout=4'b0100 then 4>1>2>3
4'b1xxx: gnt=4'b1000;
4'b0xx1: gnt=4'b0001;
4'b0x10: gnt=4'b0010;
4'b0100: gnt=4'b0100;
default: gnt=4'b0000;
endcase
$display($time," :Ref_model : request = %b, ack = %b , grant = %b, reset = %b, token = %b",req[3:0],ack,gnt[3:0],rst,ringout[3:0]);
end
endmodule
Arbiter Top module code:
`timescale 1ns / 1ps
//top module
module top();
//Clock generation 50MHz
bit clk;
always #20 clk = ~clk;
//DUT interface instantioation
arb_inf arb_dut_inf(clk);
//Reference model instantiation
arb_refm arb_refm_inf(clk);
//Environment instantioation
testcase TC(arb_dut_inf,arb_refm_inf);
//DUT instantiation
arbiter DUT (.clk(clk),.rst(arb_dut_inf.rst),.ack(arb_dut_inf.ack),.req(arb_dut_inf.req),.grant(arb_dut_inf.gnt),.token_ring(arb_dut_inf.token));
//Reference model interface instantiation
reference_model RM(.clk(clk),.ack(arb_refm_inf.ack),.rst(arb_refm_inf.rst),.gnt(arb_refm_inf.gnt),.req(arb_refm_inf.req));
initial
begin
//applying reset
arb_dut_inf.rst<=0;
arb_refm_inf.rst<=0;
$display($time,": before reset");
@(posedge arb_dut_inf.arb_bfm_cb);
//removing reset after
arb_dut_inf.rst<=1;
arb_refm_inf.rst<=1;
$display($time,": after reset");
end
endmodule
DUT interface code :
//DUT interfaces
interface arb_inf (input bit clk);
logic [3:0] req;
bit rst;
logic [3:0] gnt;
bit ack;
logic [3:0] token;
//clocking block for DUT input interface
clocking arb_bfm_cb @(posedge clk);
output req;
output ack;
endclocking : arb_bfm_cb
//clocking block for DUT output interface
clocking arb_mon_cb @(posedge clk);
input gnt;
input ack;
input req;
input token;
endclocking : arb_mon_cb
modport MON (clocking arb_mon_cb); //direction for DUT output interface
modport BFM (clocking arb_bfm_cb); //direction for DUT input interface
endinterface : arb_inf
Reference model Interface code :
//Reference model interface
interface arb_refm (input bit clk);
logic [3:0] req;
logic [3:0] gnt;
bit ack;
bit rst;
logic [3:0] token;
//clocking block for ref. module input interface
clocking arb_bfm_refm @(posedge clk);
output req;
output ack;
endclocking : arb_bfm_refm
//clocking block for ref. module output interface
clocking arb_sb_refm @(posedge clk);
input req;
input ack;
input gnt;
input token;
endclocking : arb_sb_refm
modport BFM (clocking arb_bfm_refm); //define direction for Ref. model input interface
modport SCOREBOARD (clocking arb_sb_refm); //define direction for Ref. model output interface
endinterface : arb_refm
Arbitar Transaction class :
`ifndef G_TRANS
`define G_TRANS
//Transaction class
class Transaction;
randc logic [3:0] req; //Randomizing req
logic [3:0] gnt;
logic [3:0] token;
bit ack;
//Transaction class new() method
function new();
//$display("Constructor of Transaction ");
endfunction : new
//display() method
function void display ();
$display ("Transaction : request = %b,grant = %b,ack = %b",req,gnt,ack);
endfunction : display
//For deep copy
function Transaction copy();
Transaction cpy =new();
cpy.req =this.req;
cpy.gnt =this.gnt;
cpy.ack =this.ack;
cpy.token =this.token;
copy = cpy;
endfunction
endclass
`endif
Arbitar Generator class :
`include "arb_transaction"
`ifndef G_GEN
`define G_GEN
//Generator class
class Generator;
Transaction tr;
int max_trans_cnt; //define variable for maximum number transactions
int trans_cnt = 0; //incrementing till all testcases applied
mailbox #(Transaction) gen2bfm;
function new(mailbox #(Transaction) gen2bfm, int max_trans_cnt);
$display("Constructor of Generator ");
this.gen2bfm = gen2bfm;
this.max_trans_cnt = max_trans_cnt;
tr= new();
endfunction
Arbitar BFM class :
`include "arb_transaction"
`ifndef G_BFM
`define G_BFM
//BFM class
class Bfm;
Transaction tr;
mailbox #(Transaction) bfm_mailbox; //declare generator to BFM mailbox
virtual arb_inf.BFM arb_in; //declare DUT input interface
virtual arb_refm.BFM ref_in; //declare ref. model input interface
function new (mailbox #(Transaction) bfm_mailbox, virtual arb_inf.BFM arb_in,virtual arb_refm.BFM ref_in);
$display("Constructor of bfm ");
this.bfm_mailbox = bfm_mailbox;
this.ref_in = ref_in;
this.arb_in = arb_in;
endfunction : new
//run() task
task run();
forever
begin
tr=new();
bfm_mailbox.get(tr);
dut_input(tr);
end
endtask: run
//passing values to DUT and refm
task dut_input(Transaction tr);
//sending req
@(posedge arb_in.arb_bfm_cb);
arb_in.arb_bfm_cb.req[3:0]<=tr.req[3:0];
ref_in.arb_bfm_refm.req[3:0]<= tr.req[3:0];
@(arb_in.arb_bfm_cb.req[3:0]);
$display($time," :BFM : bfm sent data to DUT : request = %b ack=%b",arb_in.arb_bfm_cb.req[3:0],arb_in.arb_bfm_cb.ack);
$display($time," :BFM : bfm sent data to Refm : request = %b ack=%b",ref_in.arb_bfm_refm.req[3:0], ref_in.arb_bfm_refm.ack);
//ack==1 after two clock cycle
@(posedge arb_in.arb_bfm_cb);
@(posedge arb_in.arb_bfm_cb);
arb_in.arb_bfm_cb.ack<=1;
ref_in.arb_bfm_refm.ack<=1;
@(arb_in.arb_bfm_cb.ack);
$display($time," :BFM : bfm sent data to DUT : request = %b ack=%b",arb_in.arb_bfm_cb.req[3:0],arb_in.arb_bfm_cb.ack);
$display($time," :BFM : bfm sent data to Refm : request = %b ack=%b",ref_in.arb_bfm_refm.req[3:0], ref_in.arb_bfm_refm.ack);
//ack will remain high for one clock cycle
@(posedge arb_in.arb_bfm_cb);
arb_in.arb_bfm_cb.ack<=0;
ref_in.arb_bfm_refm.ack<=0;
endtask
endclass :Bfm
`endif
Arbitar Monitor class :
`include "arb_transaction"
`ifndef G_MONITOR
`define G_MONITOR
//Monitor class
class Monitor;
Transaction tr;
virtual arb_inf.MON arb_dut_inf; //declare DUT to monitor interface
mailbox #(Transaction) mon2sb; //define Monitor to Scoreboard mailbox
//new() method for Monitor classs
function new (virtual arb_inf.MON arb_dut_inf, mailbox #(Transaction) mon2sb);
$display("Constructor of Monitor ");
this.arb_dut_inf=arb_dut_inf;
this.mon2sb = mon2sb;
endfunction
//main() method of Monitor class
task main();
forever
begin
tr=new();
@(posedge arb_dut_inf.arb_mon_cb);
@(posedge arb_dut_inf.arb_mon_cb.ack); //block consecutive statements untill ack will be asserted
tr.ack = arb_dut_inf.arb_mon_cb.ack; //taking ack from DUT through interface
tr.req[3:0] = arb_dut_inf.arb_mon_cb.req[3:0]; //taking req from DUT through interface
tr.gnt[3:0] = arb_dut_inf.arb_mon_cb.gnt[3:0]; //taking gnt from DUT through interface
tr.token[3:0] = arb_dut_inf.arb_mon_cb.token[3:0]; //taking token from DUT through interface
mon2sb.put(tr); //putting req,token,ack,gnt into mailbox
//if(tr.ack==1)
$display($time," :Monitor : Data sent to the Scoreboard :req = %b, ack = %b, gnt = %b, token = %b",tr.req[3:0],tr.ack,tr.gnt[3:0],tr.token[3:0]);
//tr.display();
end
endtask
endclass
`endif
Arbitar Scoreboard class :
`include "arb_transaction"
`ifndef G_ScoreBoard
`define G_ScoreBoard
//Scoreboard class
class Scoreboard;
//Declaring tr1,tr2 Transaction class objects
Transaction tr1;
Transaction tr2;
int max_transaction; //define variable for maximum number of transaction
event ended;
int match; //define variable to count total numbes of successful testcases
mailbox #(Transaction) mon_sb; //declare monitor to scoreboard mailbox
virtual arb_refm.SCOREBOARD refm_out; //declare ref. medel to scoreboard interface
//new() function
function new (mailbox #(Transaction) mon_sb,virtual arb_refm.SCOREBOARD refm_out, int max_transaction);
$display("Constructor of Scoreboard ");
this.max_transaction = max_transaction;
this.mon_sb = mon_sb;
this.refm_out = refm_out;
endfunction
//Scoreboard class run() function
task run();
tr1=new();
tr2=new();
forever
begin
//@(posedge refm_out.arb_sb_refm);
@(posedge refm_out.arb_sb_refm.ack);
tr1.ack=refm_out.arb_sb_refm.ack; //checking ack from ref. model
if(tr1.ack==1)
begin
tr1.req = refm_out.arb_sb_refm.req;
tr1.gnt= refm_out.arb_sb_refm.gnt;
mon_sb.get(tr2); //taking req,ack,gnt from monitor to scoreboard mailbox
$display($time," :Scoreboard : From Refm: req_refm =%b, ack_refm =%b, gnt_refm=%b",tr1.req[3:0],tr1.ack,tr1.gnt[3:0]);
$display($time," :Scoreboard : From DUT : req =%b, ack =%b, gnt =%b",tr2.req[3:0],tr2.ack,tr2.gnt[3:0]);
if(tr1.req!=tr2.req) //comparing req(tr1.req) coming from ref. medel with req(tr2.req) coming from scoreboard.
begin
$display ($time," :ERROR : request form dut and refm doesn't match");
$display("");
end
else if(tr1.ack!=tr2.ack) //comparing ack(tr1.ack) coming from ref. medel with req(tr2.ack) coming from scoreboard.
begin
$display ($time," :ERROR : ack form dut and refm doesn't match");
$display("");
end
else if(tr1.gnt!=tr2.gnt)//comparing grant(tr1.gnt) coming from ref. medel with grant(tr2.gnt) coming from scoreboard.
begin
$display ($time," :ERROR : grant form dut and refm doesn't match");
$display("");
end
else //if req,gnt,ack is matching then testcase tested DUT functionality successfully
begin
$display ($time," :Congratulations : Testcase successfully tested");
$display("");
match++; //counting total number of successful testcases
end
end
if(--max_transaction<1) //if all testcases have been applied to DUT then end event will be triggered to stop simulation
begin
$display ($time," :Number of Succesfull testcases are : %d",match);
->ended; //end event is defined in Environment class
end
end
endtask
endclass
`endif
Arbitar Testcase program :
//Testcase
program testcase(arb_inf arb_in,arb_refm ref_in);
`include "arb_environment"
Environment env_obj;
initial
begin
$display(" ******************* Start of testcase ****************");
//creating environment class instant(object),passing DUT and ref. medel interfaces
env_obj=new(arb_in,ref_in);
//calling run() procedure of environment class
env_obj.run();
$display(" ******************* End of testcase ****************");
end
endprogram
Arbitar Environment class :
`include "arb_transaction"
`include "arb_generator"
`include "arb_scoreboard"
`include "arb_bfm"
`include "arb_monitor"
`ifndef G_ENV
`define G_ENV
class Environment ;
//declare DUT and Ref. model interfaces
virtual arb_inf arb_dut_inf;
virtual arb_refm arb_reference;
mailbox #(Transaction) mon2sb; //declare monitor_to_scoreboard mailbox
mailbox #(Transaction) gen2bfm; //declare generator_to_bfm mailbox
//declare class objects
Generator gen_obj;
Bfm bfm_obj;
Scoreboard sb_obj;
Monitor mon_obj;
function new(virtual arb_inf arb_dut_inf,
virtual arb_refm arb_reference);
$display("Constructor of Environment");
this.arb_dut_inf = arb_dut_inf;
this.arb_reference = arb_reference;
mon2sb=new() ;
gen2bfm=new() ;
gen_obj=new(gen2bfm,16);
bfm_obj=new(gen2bfm,arb_dut_inf,arb_reference);
mon_obj=new(arb_dut_inf,mon2sb);
sb_obj =new(mon2sb,arb_reference,16);
endfunction : new
//define Environment before_test() method
virtual task before_test();
fork
sb_obj.run();
bfm_obj.run();
mon_obj.main();
join_none
endtask
//define Environment start_test() method
virtual task start_test();
fork
gen_obj.run();
join_none
endtask
//define Environment after_test() method
virtual task after_test();
fork
wait(sb_obj.ended.triggered); //wait till all the testcases have been applied
$display("end event triggered");
join
$display("End of simulation");
endtask
//define Environment class run() method
task run();
before_test();
start_test();
after_test();
endtask
endclass
`endif
Saturday, April 28, 2012
Digital Clock on Spartan3E FPGA
//Top module
module Digital_clock(clk1, segA, segB, segC, segD, segE, segF, segG, segDP,anode,hour4,hour3,hour2,hour1,hour0);
input clk1;
output segA, segB, segC, segD, segE, segF, segG, segDP,hour4,hour3,hour2,hour1,hour0;
output [3:0] anode;
wire click;
reg [25:0] count;
always @(posedge clk1)
if(count==26'd50000000)
count<=25'd0;
else
count<=count+26'h1;
wire countovf = (count==26'd50000000);
// anodes of 7segs
wire [1:0] digit_refresh= count[13:12];
assign anode[0] = ~(digit_refresh==2'h0);
assign anode[1] = ~(digit_refresh==2'h1);
assign anode[2] = ~(digit_refresh==2'h2);
assign anode[3] = ~(digit_refresh==2'h3);
//Instantiation from BCD4 module
wire [15:0] BCD_digits;
BCD_display BCD(.clk(clk1), .ena(countovf), .BCD_digits(BCD_digits),.carryout_h(click)); // Instantiation from BCD4 module
//Multiplexing for 7 segments
reg [3:0] BCDdigit;
always @(posedge clk1)
case(digit_refresh)
2'd0: BCDdigit = BCD_digits[3:0];
2'd1: BCDdigit = BCD_digits[7:4];
2'd2: BCDdigit = BCD_digits[11:8];
2'd3: BCDdigit = BCD_digits[15:12];
endcase
// 7-segments decoder
reg [7:0] SevenSeg;
always @(posedge clk1)
case(BCDdigit)
4'h0: SevenSeg = 8'b00000011; //0
4'h1: SevenSeg = 8'b10011111; //1
4'h2: SevenSeg = 8'b00100101; //2
4'h3: SevenSeg = 8'b00001101; //3
4'h4: SevenSeg = 8'b10011001; //4
4'h5: SevenSeg = 8'b01001001; //5
4'h6: SevenSeg = 8'b01000001; //6
4'h7: SevenSeg = 8'b00011111; //7
4'h8: SevenSeg = 8'b00000001; //8
4'h9: SevenSeg = 8'b00001001; //9
default: SevenSeg = 8'b11111110; //default state
endcase
assign {segA, segB, segC, segD, segE, segF, segG, segDP} = SevenSeg; //giving SevenSeg to individual 7 segment pins
//for Hour LEDs
reg [4:0] hour;
wire rollover=(hour==5'd23);
always @(posedge clk1)
if(click)
begin
if(rollover)
hour<=5'd0;
else
hour<=hour+5'd1;
end
assign {hour4,hour3,hour2,hour1,hour0}=hour; //assigning hour to individual hour pins;
endmodule
//BCD_display module
module BCD_display(clk, ena, BCD_digits,carryout_h);
input clk, ena;
output [15:0] BCD_digits;
wire [4:0] carryout;
output carryout_h;
assign carryout[0] = ena;
BCD1 digit0(.clk(clk), .ena(carryout[0]), .BCD_digit(BCD_digits[3:0]),.BCD_carryout1(carryout[1]));
BCD2 digit1(.clk(clk), .ena(carryout[1]), .BCD_digit(BCD_digits[7:4]),.BCD_carryout2(carryout[2]));
BCD3 digit2(.clk(clk), .ena(carryout[2]), .BCD_digit(BCD_digits[11:8]),.BCD_carryout3(carryout[3]));
BCD4 digit3(.clk(clk), .ena(carryout[3]), .BCD_digit(BCD_digits[15:12]),.BCD_carryout4(carryout[4]));
assign carryout_h=carryout[4];
endmodule
// BCD1 module for MSB 7seg , with carryout logic.
module BCD1(clk,ena, BCD_digit,BCD_carryout1);
input clk, ena;
output BCD_digit,BCD_carryout1;
reg [3:0] BCD_digit;
wire BCD_rollover = (BCD_digit==4'd9);
always @(posedge clk)
if(ena)
begin
if(BCD_rollover)
BCD_digit<=4'd0;
else BCD_digit<=BCD_digit+4'd1;
end
assign BCD_carryout1 = ena & BCD_rollover;
endmodule
// BCD2 module for 2nd 7seg , with carryout logic.
module BCD2(clk, ena, BCD_digit,BCD_carryout2);
input clk, ena;
output BCD_digit,BCD_carryout2;
reg [3:0] BCD_digit;
wire BCD_rollover = (BCD_digit==4'd5);
always @(posedge clk)
if(ena)
begin
if(BCD_rollover)
BCD_digit<=4'd0;
else BCD_digit<=BCD_digit+4'd1;
end
assign BCD_carryout2 = ena & BCD_rollover;
endmodule
// BCD3 for 3rd 7seg , with carryout logic.
module BCD3(clk, ena, BCD_digit,BCD_carryout3);
input clk, ena;
output BCD_digit,BCD_carryout3;
reg [3:0] BCD_digit;
wire BCD_rollover =((BCD_digit==4'd9));
always @(posedge clk)
if(ena)
begin
if(BCD_rollover)
BCD_digit<=4'd0;
else BCD_digit<=BCD_digit+4'd1;
end
assign BCD_carryout3 = (ena & BCD_rollover);
endmodule
// BCD4 for 4th 7seg , with carryout logic.
module BCD4(clk, ena, BCD_digit,BCD_carryout4);// when BCD5 will be rollover BCD_carryout4 will be high;
input clk, ena;
output BCD_digit,BCD_carryout4;
reg [3:0] BCD_digit;
wire BCD_rollover = (BCD_digit==4'd5);
always @(posedge clk)
if(ena)
begin
if(BCD_rollover)
BCD_digit<=4'd0;
else BCD_digit<=BCD_digit+4'd1;
end
assign BCD_carryout4 = ena & BCD_rollover;
endmodule
module Digital_clock(clk1, segA, segB, segC, segD, segE, segF, segG, segDP,anode,hour4,hour3,hour2,hour1,hour0);
input clk1;
output segA, segB, segC, segD, segE, segF, segG, segDP,hour4,hour3,hour2,hour1,hour0;
output [3:0] anode;
wire click;
reg [25:0] count;
always @(posedge clk1)
if(count==26'd50000000)
count<=25'd0;
else
count<=count+26'h1;
wire countovf = (count==26'd50000000);
// anodes of 7segs
wire [1:0] digit_refresh= count[13:12];
assign anode[0] = ~(digit_refresh==2'h0);
assign anode[1] = ~(digit_refresh==2'h1);
assign anode[2] = ~(digit_refresh==2'h2);
assign anode[3] = ~(digit_refresh==2'h3);
//Instantiation from BCD4 module
wire [15:0] BCD_digits;
BCD_display BCD(.clk(clk1), .ena(countovf), .BCD_digits(BCD_digits),.carryout_h(click)); // Instantiation from BCD4 module
//Multiplexing for 7 segments
reg [3:0] BCDdigit;
always @(posedge clk1)
case(digit_refresh)
2'd0: BCDdigit = BCD_digits[3:0];
2'd1: BCDdigit = BCD_digits[7:4];
2'd2: BCDdigit = BCD_digits[11:8];
2'd3: BCDdigit = BCD_digits[15:12];
endcase
// 7-segments decoder
reg [7:0] SevenSeg;
always @(posedge clk1)
case(BCDdigit)
4'h0: SevenSeg = 8'b00000011; //0
4'h1: SevenSeg = 8'b10011111; //1
4'h2: SevenSeg = 8'b00100101; //2
4'h3: SevenSeg = 8'b00001101; //3
4'h4: SevenSeg = 8'b10011001; //4
4'h5: SevenSeg = 8'b01001001; //5
4'h6: SevenSeg = 8'b01000001; //6
4'h7: SevenSeg = 8'b00011111; //7
4'h8: SevenSeg = 8'b00000001; //8
4'h9: SevenSeg = 8'b00001001; //9
default: SevenSeg = 8'b11111110; //default state
endcase
assign {segA, segB, segC, segD, segE, segF, segG, segDP} = SevenSeg; //giving SevenSeg to individual 7 segment pins
//for Hour LEDs
reg [4:0] hour;
wire rollover=(hour==5'd23);
always @(posedge clk1)
if(click)
begin
if(rollover)
hour<=5'd0;
else
hour<=hour+5'd1;
end
assign {hour4,hour3,hour2,hour1,hour0}=hour; //assigning hour to individual hour pins;
endmodule
//BCD_display module
module BCD_display(clk, ena, BCD_digits,carryout_h);
input clk, ena;
output [15:0] BCD_digits;
wire [4:0] carryout;
output carryout_h;
assign carryout[0] = ena;
BCD1 digit0(.clk(clk), .ena(carryout[0]), .BCD_digit(BCD_digits[3:0]),.BCD_carryout1(carryout[1]));
BCD2 digit1(.clk(clk), .ena(carryout[1]), .BCD_digit(BCD_digits[7:4]),.BCD_carryout2(carryout[2]));
BCD3 digit2(.clk(clk), .ena(carryout[2]), .BCD_digit(BCD_digits[11:8]),.BCD_carryout3(carryout[3]));
BCD4 digit3(.clk(clk), .ena(carryout[3]), .BCD_digit(BCD_digits[15:12]),.BCD_carryout4(carryout[4]));
assign carryout_h=carryout[4];
endmodule
// BCD1 module for MSB 7seg , with carryout logic.
module BCD1(clk,ena, BCD_digit,BCD_carryout1);
input clk, ena;
output BCD_digit,BCD_carryout1;
reg [3:0] BCD_digit;
wire BCD_rollover = (BCD_digit==4'd9);
always @(posedge clk)
if(ena)
begin
if(BCD_rollover)
BCD_digit<=4'd0;
else BCD_digit<=BCD_digit+4'd1;
end
assign BCD_carryout1 = ena & BCD_rollover;
endmodule
// BCD2 module for 2nd 7seg , with carryout logic.
module BCD2(clk, ena, BCD_digit,BCD_carryout2);
input clk, ena;
output BCD_digit,BCD_carryout2;
reg [3:0] BCD_digit;
wire BCD_rollover = (BCD_digit==4'd5);
always @(posedge clk)
if(ena)
begin
if(BCD_rollover)
BCD_digit<=4'd0;
else BCD_digit<=BCD_digit+4'd1;
end
assign BCD_carryout2 = ena & BCD_rollover;
endmodule
// BCD3 for 3rd 7seg , with carryout logic.
module BCD3(clk, ena, BCD_digit,BCD_carryout3);
input clk, ena;
output BCD_digit,BCD_carryout3;
reg [3:0] BCD_digit;
wire BCD_rollover =((BCD_digit==4'd9));
always @(posedge clk)
if(ena)
begin
if(BCD_rollover)
BCD_digit<=4'd0;
else BCD_digit<=BCD_digit+4'd1;
end
assign BCD_carryout3 = (ena & BCD_rollover);
endmodule
// BCD4 for 4th 7seg , with carryout logic.
module BCD4(clk, ena, BCD_digit,BCD_carryout4);// when BCD5 will be rollover BCD_carryout4 will be high;
input clk, ena;
output BCD_digit,BCD_carryout4;
reg [3:0] BCD_digit;
wire BCD_rollover = (BCD_digit==4'd5);
always @(posedge clk)
if(ena)
begin
if(BCD_rollover)
BCD_digit<=4'd0;
else BCD_digit<=BCD_digit+4'd1;
end
assign BCD_carryout4 = ena & BCD_rollover;
endmodule
.ucf file for Digital clock-xc3s250e-5cp132
# clock pins for Basys2 Board
NET "clk1" LOC = "B8"; # Bank = 0, Signal name = MCLK
//NET "clk" LOC = "M6"; # Bank = 2, Signal name = UCLK
NET "clk1" CLOCK_DEDICATED_ROUTE = FALSE;
# Connected to Basys2 onBoard 7seg display
NET "segA" LOC = "L14"; # Bank = 1, Signal name = CA
NET "segB" LOC = "H12"; # Bank = 1, Signal name = CB
NET "segC" LOC = "N14"; # Bank = 1, Signal name = CC
NET "segD" LOC = "N11"; # Bank = 2, Signal name = CD
NET "segE" LOC = "P12"; # Bank = 2, Signal name = CE
NET "segF" LOC = "L13"; # Bank = 1, Signal name = CF
NET "segG" LOC = "M12"; # Bank = 1, Signal name = CG
NET "SegDP"LOC = "N13"; # Bank = 1, Signal name = DP
#anode pin assignments
NET "anode<3>" LOC = "K14"; # Bank = 1, Signal name = AN3
NET "anode<2>" LOC = "M13"; # Bank = 1, Signal name = AN2
NET "anode<1>" LOC = "J12"; # Bank = 1, Signal name = AN1
NET "anode<0>" LOC = "F12"; # Bank = 1, Signal name = AN0
# Pin assignment for LEDs
NET "hour4" LOC = "N5" ; # Bank = 2, Signal name = LD4
NET "hour3" LOC = "P6" ; # Bank = 2, Signal name = LD3
NET "hour2" LOC = "P7" ; # Bank = 3, Signal name = LD2
NET "hour1" LOC = "M11" ; # Bank = 2, Signal name = LD1
NET "hour0" LOC = "M5" ; # Bank = 2, Signal name = LD0
Thursday, April 26, 2012
.ucf file for EVM
# clock pins for Basys2 Board
NET "clk" LOC = "B8"; # Bank = 0, Signal name = MCLK
//NET "clk" LOC = "M6"; # Bank = 2, Signal name = UCLK
NET "clk" CLOCK_DEDICATED_ROUTE = FALSE;
# Connected to Basys2 onBoard 7seg display
NET "segA" LOC = "L14"; # Bank = 1, Signal name = CA
NET "segB" LOC = "H12"; # Bank = 1, Signal name = CB
NET "segC" LOC = "N14"; # Bank = 1, Signal name = CC
NET "segD" LOC = "N11"; # Bank = 2, Signal name = CD
NET "segE" LOC = "P12"; # Bank = 2, Signal name = CE
NET "segF" LOC = "L13"; # Bank = 1, Signal name = CF
NET "segG" LOC = "M12"; # Bank = 1, Signal name = CG
NET "SegDP"LOC = "N13"; # Bank = 1, Signal name = DP
NET "anode<3>" LOC = "K14"; # Bank = 1, Signal name = AN3
NET "anode<2>" LOC = "M13"; # Bank = 1, Signal name = AN2
NET "anode<1>" LOC = "J12"; # Bank = 1, Signal name = AN1
NET "anode<0>" LOC = "F12"; # Bank = 1, Signal name = AN0
NET "switch_2" LOC = "A7"; # Bank = 0, Signal name = BTN0
NET "switch_1" LOC = "M4"; # Bank = 0, Signal name = BTN2
NET "reset_1" LOC = "K3"; # Bank = 2, Signal name = BTN1
NET "reset_2" LOC = "B4"; # Bank = 1, Signal name = BTN3
NET "cntrl" LOC = "P11"; # Bank = 2, Signal name = SW0
NET "master_reset" LOC = "L3"; # Bank = 2, Signal name = SW1
NET "sw4" LOC = "N3"; # Bank = 2, Signal name = SW0
NET "led1" LOC = "M11" ; # Bank = 2, Signal name = LD1
NET "led2" LOC = "N4" ; # Bank = 2, Signal name = LD1
Electronic Voting Machine on Spartan3E FPGA boad
`timescale 1ns / 1ps
//top module
module voting_machine(input clk,switch_1,switch_2,reset_1,reset_2,cntrl,master_reset,sw4,output segA, segB, segC, segD, segE, segF, segG, segDP,led1,led2, output [3:0] anode);
wire q,and1,and2;
LED_SevenSeg LED_instance(.clk(clk),.switch_1(and1),.switch_2(and2),.reset_1(reset_1),.reset_2(reset_2),.cntrl(cntrl),.master_reset(master_reset)
,.segA(segA),.segB(segB),.segC(segC),.segD(segD),.segE(segE),.segF(segF),.segG(segG),.segDP(segDP),.anode(anode),.sw_44(sw4)); //Instance of Led-seven seg module
assign led1=q;
assign led2=q;
cntrl control(.clk1(clk),.switch1(switch_1),.switch2(switch_2),.reset(sw4),.and11(and1),.and22(and2),.Q(q));// Instance of cntrl module
endmodule
//ctrl module
module cntrl(input clk1, switch1,switch2,reset,output and11,and22,Q);
PushButton_Debouncer p1(.clk2(clk1),.PB(~switch1),.PB_down(swt1));
PushButton_Debouncer p2(.clk2(clk1),.PB(~switch2),.PB_down(swt2));
wire Q_bar;
xor x1(xor_out,swt2,swt1);
JK_ff flipflop(.clk(xor_out),.reset(reset),.J(Q_bar),.K(Q_bar),.q1(Q),.q_bar(Q_bar));
//for and1
and a1(and1,switch1,Q_bar);
not n1(out_switch2,switch2);
and a11(and11,and1,out_switch2);
//for and2
and a2(and2,switch2,Q_bar);
not n2(out_switch1,switch1);
and a22(and22,and2,out_switch1);
endmodule
//JK_ff module
module JK_ff(input clk,reset,J,K,output q1, output q_bar);
reg q;
always @(posedge clk or posedge reset)
if(reset)
q<=0;
else
q<=(J & ~q)|(~K & q);
assign q_bar=~q;
assign q1=q;
endmodule
//debounce module
module PushButton_Debouncer(clk2, PB, PB_down);
input clk2;
input PB;
output PB_down; // 1 when the push-button goes down.
// use two flipflops to synchronize the PB signal with the clock domain
reg PB_sync_0; always @(posedge clk2) PB_sync_0 <= ~PB; // invert PB to make PB_sync_0 active high
reg PB_sync_1; always @(posedge clk2) PB_sync_1 <= PB_sync_0;
reg [23:0] PB_cnt;
reg PB_state; // When the push-button,we increment the counter
initial
PB_state=0; // state of the push-button (0 when up, 1 when down)
wire PB_idle = (PB_state==PB_sync_1);
wire PB_cnt_max = &PB_cnt; // The counter has to reach at maximum count before we decide that the push-button state has changed.
always @(posedge clk2)
if(PB_idle)
PB_cnt <= 0;
else
begin
PB_cnt <= PB_cnt + 1; // increment the counter
if(PB_cnt_max) PB_state <= ~PB_state; // if the counter is maxed out, state should be changed
end
wire PB_down = ~PB_state & ~PB_idle & PB_cnt_max; // true for one clock cycle when we detect that PB went down
endmodule
//seven segment display module
module LED_SevenSeg(clk,switch_1,switch_2,reset_1,reset_2,cntrl,master_reset,segA, segB, segC, segD, segE, segF, segG, segDP,anode,sw_44);
input clk,switch_1,switch_2,reset_1,reset_2,cntrl,master_reset,sw_44;
output segA, segB, segC, segD, segE, segF, segG, segDP;
output [3:0] anode;
reg [23:0] count;
always @(posedge clk) count<=count+24'h1;
wire countovf = &count;
// anodes of 7segs
wire [1:0] digit_refresh= count[17:16];
assign anode[0] = ~(digit_refresh==2'h0);
assign anode[1] = ~(digit_refresh==2'h1);
assign anode[2] = ~(digit_refresh==2'h2);
assign anode[3] = ~(digit_refresh==2'h3);
// 4 BCD digits
wire [15:0] BCD_digits;
BCD4 BCD(.clk(clk),.switch1(switch_1),.switch2(switch_2),.reset1(reset_1),.reset2(reset_2),.ena(countovf),.con(cntrl),.BCD_digits(BCD_digits),.m_rst(master_reset),.sw_4(sw_44)); //Instance of BCD 4 module
// multiplexer
reg [3:0] BCDdigit;
always @(posedge clk)
case(digit_refresh)
2'd0: BCDdigit = BCD_digits[ 3: 0];
2'd1: BCDdigit = BCD_digits[ 7: 4];
2'd2: BCDdigit = BCD_digits[11: 8];
2'd3: BCDdigit = BCD_digits[15:12];
endcase
// 7-segments decoder
reg [7:0] SevenSeg;
always @(posedge clk)
case(BCDdigit)
4'h0: SevenSeg = 8'b00000011;
4'h1: SevenSeg = 8'b10011111;
4'h2: SevenSeg = 8'b00100101;
4'h3: SevenSeg = 8'b00001101;
4'h4: SevenSeg = 8'b10011001;
4'h5: SevenSeg = 8'b01001001;
4'h6: SevenSeg = 8'b01000001;
4'h7: SevenSeg = 8'b00011111;
4'h8: SevenSeg = 8'b00000001;
4'h9: SevenSeg = 8'b00001001;
default: SevenSeg = 8'b11111110;
endcase
assign {segA, segB, segC, segD, segE, segF, segG, segDP} = SevenSeg;
endmodule
// BCD4 module
module BCD4(clk,switch1,switch2,reset1,reset2,con, ena, BCD_digits,m_rst,sw_4);
input clk,switch1,switch2,reset1,reset2,con,ena,m_rst,sw_4;
output [15:0] BCD_digits;
wire [2:0] carryout;
assign carryout[0] =ena ;
BCD1 digit0(.clk(clk),.switch(switch1),.reset(reset1),.control(con), .ena(carryout[0]),.BCD_digit(BCD_digits[3:0]),.BCD_carryout(carryout[1]),.m_reset(m_rst),.sw(sw_4));
BCD1 digit1(.clk(clk),.switch(switch1),.reset(reset1),.control(con), .ena(carryout[1]),.BCD_digit(BCD_digits[7:4]),.m_reset(m_rst),.sw(sw_4));
BCD1 digit2(.clk(clk),.switch(switch2),.reset(reset2),.control(con),.ena(carryout[0]),.BCD_digit(BCD_digits[11:8]), .BCD_carryout(carryout[2]),.m_reset(m_rst),.sw(sw_4));
BCD1 digit3(.clk(clk),.switch(switch2),.reset(reset2),.control(con),.ena(carryout[2]),.BCD_digit(BCD_digits[15:12]),.m_reset(m_rst),.sw(sw_4));
endmodule
// BCD1 for 1 7seg , with carryout logic. // This is the beauty of the code..!!
module BCD1(clk,switch,reset,control,ena,BCD_digit, BCD_carryout,m_reset,sw);
input clk,switch,reset,control,ena,m_reset,sw;
output [3:0] BCD_digit;
output BCD_carryout;
reg [3:0] BCD_digit;
wire BCD_rollover = (BCD_digit==4'd9);
assign BCD_carryout = ena & BCD_rollover;
reg con;
always @(posedge clk)
begin
if(control ==1'b0)
BCD_digit<=BCD_digit;
if(sw==0)
con<=1;
if(sw==1)
con<=0;
if((reset && control)||(control && m_reset)==1'b1)
BCD_digit<=4'b0;
if(switch && ena && control && con )
begin
if(BCD_rollover)
BCD_digit<=4'd0;
else
BCD_digit<=BCD_digit+4'd1;
con<=~con;
end
end
endmodule
Thursday, March 29, 2012
48 Operations 4-bit Microprocessor Prototype-Synthesis code
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: Home user
// Engineer: Ashik Ghona
//
// Design Name: Processor functionality code
// Module Name: fa_ALU //top_module
// Project Name: 4-bit Microprocessor processor
// Target Devices:
// Tool versions: Xilinx ISE 13.4 and lesser compatible versions
// Description: This code is created to write functionalities of 4-bit microprocessors.
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
//Module for XOR_Gate/////////////////////////////////////////////////////////////////
module xor_gate(a,b,y);
input a,b;
output y;
reg y;
always@(a,b)
begin
if (a==b)
y=0;
else
y=1;
end
endmodule
////////////////////////////////////////////////////////////////////////////////
//Module for 4bit mux16_4 having 4bit_bus I/O///////////////////////////////////
module mux16_4(input [3:0] i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15,i16,input s3,s2,s1,s0,output reg [3:0] out);
always@(i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15,i16,s0,s1,s2,s3) //s1,s0 for select line, i1,i2,i3,i4 inputs
begin
case({s3,s2,s1,s0})
4'b0000 : out=i1;
4'b0001 : out=i2;
4'b0010 : out=i3;
4'b0011 : out=i4;
4'b0100 : out=i5;
4'b0101 : out=i6;
4'b0110 : out=i7;
4'b0111 : out=i8;
4'b1000 : out=i9;
4'b1001 : out=i10;
4'b1010 : out=i11;
4'b1011 : out=i12;
4'b1100 : out=i13;
4'b1101 : out=i14;
4'b1110 : out=i15;
4'b1111 : out=i16;
endcase
end
endmodule
/////////////////////////////////////////////////////////////////////////////////
//Module for 4bit bus mux21_4 having 4bit_bus I/O////////////////////////////////
module mux21_4(input [3:0] i1,i2,input m,output reg [3:0] out);
always@(m,i1,i2) //m for select line, i1,i2 inputs
begin
case(m)
1'b0 : out=i1;
1'b1 : out=i2;
endcase
end
endmodule
/////////////////////////////////////////////////////////////////////////////////
//Module for 1bit Full adder/////////////////////////////////////////////////////
module fa_1_bit(a,b,cin,s,cout);
input a,b;
input cin;
output s,cout;
wire w1;
xor_gate xor1(a,b,w1); //Instantiation from xor_gate module
xor_gate xor2(w1,cin,s); //Instantiation from xor_gate module
and and1(o1,w1,cin);
and and2(o2,a,b);
or or1(cout,o1,o2);
endmodule
/////////////////////////////////////////////////////////////////////////////////
//Module for 1bit Full adder/////////////////////////////////////////////////////
module alu_4bit(a,b,cin,sel,M,cout,temp_result);
input [3:0] a,b;
input [3:0] sel;
input M;
input cin;
output [3:0] cout,temp_result;
wire [3:0] ba,bb;
wire [3:0] tsl1,tsl2,tsl3,tsl4,tsl5,tsl6,tsl7,tsl8,tsl9,tsl10,tsl11,tsl12,tsl13,tsl14,tsl15,tsl16,tl,sum;
//Block for arithmetic operations
//Instantiation from mux16_4 having 4bit_bus I/O for A
mux16_4 mux16_arithmetic_A(a,tsl15,tsl14,4'b0000,a,tsl15,a,tsl8,a,a,tsl14,tsl12,a,tsl15,tsl14,a,sel[3],sel[2],sel[1],sel[0],ba); //ba is the output of mux_41_arithmetic
//Instantiation from mux16_4 having 4bit_bus I/O for B
mux16_4 mux16_arithmetic_B(4'b1111,4'b1111,4'b1111,4'b1111,tsl8,tsl8,~(b-1)+4'b0001,4'b0000,tsl12,b,tsl12,4'b0000,4'b0000,a,a,4'b0000,sel[3],sel[2],sel[1],sel[0],bb);//bb is the output of mux_41_arithmetic
//Instantiation from fa_1_bit module
fa_1_bit fadder1(ba[0],bb[0],cin,sum[0],cout[0]);
fa_1_bit fadder2(ba[1],bb[1],cout[0],sum[1],cout[1]);
fa_1_bit fadder3(ba[2],bb[2],cout[1],sum[2],cout[2]);
fa_1_bit fadder4(ba[3],bb[3],cout[2],sum[3],cout[3]); //s[3:1] is the output of this 4bit adder block
//Block for logical operation
assign tsl1=~a;
assign tsl2=~(a&b);
assign tsl3=~(a)&b;
assign tsl4=1;
assign tsl5=~(a|b);
assign tsl6=~b;
assign tsl7=~(a^b);
assign tsl8=a|(~b);
assign tsl9=~a&b;
assign tsl10=a^b;
assign tsl11=b;
assign tsl12=a|b;
assign tsl13=0;
assign tsl14=a&~b;
assign tsl15=a&b;
assign tsl16=a;
//Instantiation from mux16_4 having 4bit_bus I/O
mux16_4 mux16_logical(tsl1,tsl2,tsl3,tsl4,tsl5,tsl6,tsl7,tsl8,tsl9,tsl10,tsl11,tsl12,tsl13,tsl14,tsl15,tsl16,sel[3],sel[2],sel[1],sel[0],tl);
//Instantiation from mux41_4 having 4bit_bus I/O
//mux41_4 mux41_logical(tsl1,tsl2,tsl3,tsl4,sel[1],sel[0],tl); //tl is the output of mux41_logical
//Instantiation from mux21_4 having 4bit_bus I/O
mux21_4 alu_output(sum,tl,M,temp_result); //temp_result is the result of alu_output
endmodule
//////////////////////////////////////////////////////////////////////////////
//Module 4-bit Registered ALU/////////////////////////////////////////////////
module reg_alu_top_module(a,b,cin,sel,M,sum,clk,rst);
input [3:0] a,b;
input [3:0] sel;
input cin,clk,M;
input rst;
output reg [3:0] sum;
wire [3:0] result;
wire [3:0] cout;
alu_4bit alu(a,b,cin,sel,M,cout,result);
always@(posedge clk or negedge rst)
if(!rst)
sum<=4'b0000;
else
begin
sum<=result; //sum is the result of reg_alu_top_module
end
endmodule
///////////////////////////////////////////////////////////////////////////////
//Instruction Decoder Module//////////////////////////////////////////////////
// This module is the brain of the processor.
// It is implemented using Moore model FSM.
module instr_decoder_fsm(
input clk, rst,cin,
input [3:0] oper1,oper2,datain,aluout,
input [3:0] opcode,
output reg [3:0] address,aluin1,aluin2,dataout,
output reg [3:0] selalu,
output reg rwn,chipsel,
output reg [1:0] state,
output reg cin_out
);
reg [1:0] current_state;
reg [1:0] next_state;
parameter idle = 2'b00,
fetch = 2'b01,
execute = 2'b10,
load = 2'b11;
// always block for State transition logic
always @(posedge clk or negedge rst)
if (!rst)
current_state <= idle;
else
current_state <= next_state;
// always block Next state logic
always @(current_state)
begin
next_state = 2'bx;
case(current_state)
idle: next_state = fetch;
fetch: next_state = execute ;
execute: next_state = load;
load: next_state = fetch;
endcase
end
//always for Output logic
always @(current_state,aluout)
begin
case (current_state)
idle:
begin
state=idle;
chipsel=1'b1;
rwn=1'bx;
address=oper1;
aluin1=4'b0000;
aluin2=4'b0000;
cin_out=1'b0;
dataout=4'b0000;
selalu=4'b0000;
end
fetch:
begin
state=fetch;
chipsel=1'b0;
rwn=1'b1; //rwn=1 for reading memory
address=oper1; // put the address(oper1) to read data from that memory location of RAM
dataout=4'bzzzz;
aluin1=datain;
aluin2=oper2;
cin_out=cin;
selalu=opcode;
end
execute:
begin
state=execute;
selalu=opcode; //giving opcode for operations to ALU
address=oper1;
aluin1=datain; //Giving datain(which was read fro RAM in previous cycle) to ALU
aluin2=oper2; // Giving oper2 to ALU
cin_out=cin; //Giving cin to ALU.
chipsel=1'b0;
rwn=1'bz;
dataout=4'b0000;
end
load:
begin
state=load;
address=oper1; // Put the address(oper1) for RAM locaton
dataout=aluout; // put the ALU output on dataout
rwn=1'b0; //Writing ALU output to RAM
chipsel=1'b0; //selecting RAM
selalu=3'bzzz;
aluin1=4'bzzzz;
aluin2=4'bzzzz;
cin_out=1'bz;
end
endcase
end
endmodule
///////////////////////////////////////////////////////////////////////////////
//Data_RAM module//////////////////////////////////////////////////////////////
module dataram(dataout, addr, datain, csn, rwn);
input [3:0] addr;
input [3:0] datain;
input csn;
input rwn;
output reg [3:0] dataout;
reg [3:0] data [15:0];
always@(rwn,datain,csn,addr)
begin
if(csn == 1)
begin
data[0] = 0;
data[1] = 1;
data[2] = 2;
data[3] = 3;
data[4] = 4;
data[5] = 5;
data[6] = 6;
data[7] = 7;
data[8] = 8;
data[9] = 9;
data[10] = 10;
data[11] = 11;
data[12] = 12;
data[13] = 13;
data[14] = 14;
data[15] = 15;
end
if(csn==0 && rwn==1)
begin
dataout = data[addr];
end
else if(csn==0 && rwn==0)
begin
data[addr] = datain;
dataout = 4'bzzzz;
end
else
begin
dataout = 4'bzzzz;
end
end
endmodule
//////////////////////////////////////////////////////////////////////////////////
//Top module for 4-bit Microprocessor/////////////////////////////////////////////
module fa_ALU(
input [4:0] OPCODE,
input [3:0] A,
input [3:0] B,
input M,
input CLK,
input RESET,
output [1:0] STATE_OUT,
output [3:0] sum_aluout
);
//wire [1:0] STATE_OUT; //here I'm taking STATE_OUT and sum_aluout as output just to monitor results and state of FSM
//wire [3:0] sum_aluout; //If you don't want to monitor them then just remove them from port list and declare them wire
wire [3:0] ram_out_deco_in,address,aluin1_A, aluin2_B, deco_out_ram_in;
wire [4:0] selalu_sel;
wire rwn,chipsel,cout_cin;
//Instantiation from Instr_decoder_fsm module
instr_decoder_fsm decoder(CLK,RESET,OPCODE[0],A,B,ram_out_deco_in,sum_aluout,OPCODE[4:1],address,aluin1_A,aluin2_B,deco_out_ram_in,selalu_sel,rwn,chipsel,STATE_OUT,cout_cin);
//Instantiation from fa4bit_alu_final
reg_alu_top_module reg_alu(aluin1_A,aluin2_B,cout_cin,selalu_sel,M,sum_aluout,CLK,RESET);
//Instantiation from distram module
dataram ram(ram_out_deco_in, address, deco_out_ram_in,chipsel,rwn);
endmodule
/////////////////////////////////////////////////////////////////////////////////////////////
// Code OVER
//////////////////////////////////////////////////////////////////////////////////
// Company: Home user
// Engineer: Ashik Ghona
//
// Design Name: Processor functionality code
// Module Name: fa_ALU //top_module
// Project Name: 4-bit Microprocessor processor
// Target Devices:
// Tool versions: Xilinx ISE 13.4 and lesser compatible versions
// Description: This code is created to write functionalities of 4-bit microprocessors.
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
//Module for XOR_Gate/////////////////////////////////////////////////////////////////
module xor_gate(a,b,y);
input a,b;
output y;
reg y;
always@(a,b)
begin
if (a==b)
y=0;
else
y=1;
end
endmodule
////////////////////////////////////////////////////////////////////////////////
//Module for 4bit mux16_4 having 4bit_bus I/O///////////////////////////////////
module mux16_4(input [3:0] i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15,i16,input s3,s2,s1,s0,output reg [3:0] out);
always@(i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15,i16,s0,s1,s2,s3) //s1,s0 for select line, i1,i2,i3,i4 inputs
begin
case({s3,s2,s1,s0})
4'b0000 : out=i1;
4'b0001 : out=i2;
4'b0010 : out=i3;
4'b0011 : out=i4;
4'b0100 : out=i5;
4'b0101 : out=i6;
4'b0110 : out=i7;
4'b0111 : out=i8;
4'b1000 : out=i9;
4'b1001 : out=i10;
4'b1010 : out=i11;
4'b1011 : out=i12;
4'b1100 : out=i13;
4'b1101 : out=i14;
4'b1110 : out=i15;
4'b1111 : out=i16;
endcase
end
endmodule
/////////////////////////////////////////////////////////////////////////////////
//Module for 4bit bus mux21_4 having 4bit_bus I/O////////////////////////////////
module mux21_4(input [3:0] i1,i2,input m,output reg [3:0] out);
always@(m,i1,i2) //m for select line, i1,i2 inputs
begin
case(m)
1'b0 : out=i1;
1'b1 : out=i2;
endcase
end
endmodule
/////////////////////////////////////////////////////////////////////////////////
//Module for 1bit Full adder/////////////////////////////////////////////////////
module fa_1_bit(a,b,cin,s,cout);
input a,b;
input cin;
output s,cout;
wire w1;
xor_gate xor1(a,b,w1); //Instantiation from xor_gate module
xor_gate xor2(w1,cin,s); //Instantiation from xor_gate module
and and1(o1,w1,cin);
and and2(o2,a,b);
or or1(cout,o1,o2);
endmodule
/////////////////////////////////////////////////////////////////////////////////
//Module for 1bit Full adder/////////////////////////////////////////////////////
module alu_4bit(a,b,cin,sel,M,cout,temp_result);
input [3:0] a,b;
input [3:0] sel;
input M;
input cin;
output [3:0] cout,temp_result;
wire [3:0] ba,bb;
wire [3:0] tsl1,tsl2,tsl3,tsl4,tsl5,tsl6,tsl7,tsl8,tsl9,tsl10,tsl11,tsl12,tsl13,tsl14,tsl15,tsl16,tl,sum;
//Block for arithmetic operations
//Instantiation from mux16_4 having 4bit_bus I/O for A
mux16_4 mux16_arithmetic_A(a,tsl15,tsl14,4'b0000,a,tsl15,a,tsl8,a,a,tsl14,tsl12,a,tsl15,tsl14,a,sel[3],sel[2],sel[1],sel[0],ba); //ba is the output of mux_41_arithmetic
//Instantiation from mux16_4 having 4bit_bus I/O for B
mux16_4 mux16_arithmetic_B(4'b1111,4'b1111,4'b1111,4'b1111,tsl8,tsl8,~(b-1)+4'b0001,4'b0000,tsl12,b,tsl12,4'b0000,4'b0000,a,a,4'b0000,sel[3],sel[2],sel[1],sel[0],bb);//bb is the output of mux_41_arithmetic
//Instantiation from fa_1_bit module
fa_1_bit fadder1(ba[0],bb[0],cin,sum[0],cout[0]);
fa_1_bit fadder2(ba[1],bb[1],cout[0],sum[1],cout[1]);
fa_1_bit fadder3(ba[2],bb[2],cout[1],sum[2],cout[2]);
fa_1_bit fadder4(ba[3],bb[3],cout[2],sum[3],cout[3]); //s[3:1] is the output of this 4bit adder block
//Block for logical operation
assign tsl1=~a;
assign tsl2=~(a&b);
assign tsl3=~(a)&b;
assign tsl4=1;
assign tsl5=~(a|b);
assign tsl6=~b;
assign tsl7=~(a^b);
assign tsl8=a|(~b);
assign tsl9=~a&b;
assign tsl10=a^b;
assign tsl11=b;
assign tsl12=a|b;
assign tsl13=0;
assign tsl14=a&~b;
assign tsl15=a&b;
assign tsl16=a;
//Instantiation from mux16_4 having 4bit_bus I/O
mux16_4 mux16_logical(tsl1,tsl2,tsl3,tsl4,tsl5,tsl6,tsl7,tsl8,tsl9,tsl10,tsl11,tsl12,tsl13,tsl14,tsl15,tsl16,sel[3],sel[2],sel[1],sel[0],tl);
//Instantiation from mux41_4 having 4bit_bus I/O
//mux41_4 mux41_logical(tsl1,tsl2,tsl3,tsl4,sel[1],sel[0],tl); //tl is the output of mux41_logical
//Instantiation from mux21_4 having 4bit_bus I/O
mux21_4 alu_output(sum,tl,M,temp_result); //temp_result is the result of alu_output
endmodule
//////////////////////////////////////////////////////////////////////////////
//Module 4-bit Registered ALU/////////////////////////////////////////////////
module reg_alu_top_module(a,b,cin,sel,M,sum,clk,rst);
input [3:0] a,b;
input [3:0] sel;
input cin,clk,M;
input rst;
output reg [3:0] sum;
wire [3:0] result;
wire [3:0] cout;
alu_4bit alu(a,b,cin,sel,M,cout,result);
always@(posedge clk or negedge rst)
if(!rst)
sum<=4'b0000;
else
begin
sum<=result; //sum is the result of reg_alu_top_module
end
endmodule
///////////////////////////////////////////////////////////////////////////////
//Instruction Decoder Module//////////////////////////////////////////////////
// This module is the brain of the processor.
// It is implemented using Moore model FSM.
module instr_decoder_fsm(
input clk, rst,cin,
input [3:0] oper1,oper2,datain,aluout,
input [3:0] opcode,
output reg [3:0] address,aluin1,aluin2,dataout,
output reg [3:0] selalu,
output reg rwn,chipsel,
output reg [1:0] state,
output reg cin_out
);
reg [1:0] current_state;
reg [1:0] next_state;
parameter idle = 2'b00,
fetch = 2'b01,
execute = 2'b10,
load = 2'b11;
// always block for State transition logic
always @(posedge clk or negedge rst)
if (!rst)
current_state <= idle;
else
current_state <= next_state;
// always block Next state logic
always @(current_state)
begin
next_state = 2'bx;
case(current_state)
idle: next_state = fetch;
fetch: next_state = execute ;
execute: next_state = load;
load: next_state = fetch;
endcase
end
//always for Output logic
always @(current_state,aluout)
begin
case (current_state)
idle:
begin
state=idle;
chipsel=1'b1;
rwn=1'bx;
address=oper1;
aluin1=4'b0000;
aluin2=4'b0000;
cin_out=1'b0;
dataout=4'b0000;
selalu=4'b0000;
end
fetch:
begin
state=fetch;
chipsel=1'b0;
rwn=1'b1; //rwn=1 for reading memory
address=oper1; // put the address(oper1) to read data from that memory location of RAM
dataout=4'bzzzz;
aluin1=datain;
aluin2=oper2;
cin_out=cin;
selalu=opcode;
end
execute:
begin
state=execute;
selalu=opcode; //giving opcode for operations to ALU
address=oper1;
aluin1=datain; //Giving datain(which was read fro RAM in previous cycle) to ALU
aluin2=oper2; // Giving oper2 to ALU
cin_out=cin; //Giving cin to ALU.
chipsel=1'b0;
rwn=1'bz;
dataout=4'b0000;
end
load:
begin
state=load;
address=oper1; // Put the address(oper1) for RAM locaton
dataout=aluout; // put the ALU output on dataout
rwn=1'b0; //Writing ALU output to RAM
chipsel=1'b0; //selecting RAM
selalu=3'bzzz;
aluin1=4'bzzzz;
aluin2=4'bzzzz;
cin_out=1'bz;
end
endcase
end
endmodule
///////////////////////////////////////////////////////////////////////////////
//Data_RAM module//////////////////////////////////////////////////////////////
module dataram(dataout, addr, datain, csn, rwn);
input [3:0] addr;
input [3:0] datain;
input csn;
input rwn;
output reg [3:0] dataout;
reg [3:0] data [15:0];
always@(rwn,datain,csn,addr)
begin
if(csn == 1)
begin
data[0] = 0;
data[1] = 1;
data[2] = 2;
data[3] = 3;
data[4] = 4;
data[5] = 5;
data[6] = 6;
data[7] = 7;
data[8] = 8;
data[9] = 9;
data[10] = 10;
data[11] = 11;
data[12] = 12;
data[13] = 13;
data[14] = 14;
data[15] = 15;
end
if(csn==0 && rwn==1)
begin
dataout = data[addr];
end
else if(csn==0 && rwn==0)
begin
data[addr] = datain;
dataout = 4'bzzzz;
end
else
begin
dataout = 4'bzzzz;
end
end
endmodule
//////////////////////////////////////////////////////////////////////////////////
//Top module for 4-bit Microprocessor/////////////////////////////////////////////
module fa_ALU(
input [4:0] OPCODE,
input [3:0] A,
input [3:0] B,
input M,
input CLK,
input RESET,
output [1:0] STATE_OUT,
output [3:0] sum_aluout
);
//wire [1:0] STATE_OUT; //here I'm taking STATE_OUT and sum_aluout as output just to monitor results and state of FSM
//wire [3:0] sum_aluout; //If you don't want to monitor them then just remove them from port list and declare them wire
wire [3:0] ram_out_deco_in,address,aluin1_A, aluin2_B, deco_out_ram_in;
wire [4:0] selalu_sel;
wire rwn,chipsel,cout_cin;
//Instantiation from Instr_decoder_fsm module
instr_decoder_fsm decoder(CLK,RESET,OPCODE[0],A,B,ram_out_deco_in,sum_aluout,OPCODE[4:1],address,aluin1_A,aluin2_B,deco_out_ram_in,selalu_sel,rwn,chipsel,STATE_OUT,cout_cin);
//Instantiation from fa4bit_alu_final
reg_alu_top_module reg_alu(aluin1_A,aluin2_B,cout_cin,selalu_sel,M,sum_aluout,CLK,RESET);
//Instantiation from distram module
dataram ram(ram_out_deco_in, address, deco_out_ram_in,chipsel,rwn);
endmodule
/////////////////////////////////////////////////////////////////////////////////////////////
// Code OVER
48 Operations 4-bit Microprocessor Prototype-Testbench
`timescale 1ns / 1ps
////////////////////////////////////////////////////////////////////////////////
// Company: Home user
// Engineer: Ashik Ghona
//
// Design Name: fa_ALU
// Module Name: D:/Data/Sandipani/Material/ASH/fa_ALU/fa_ALU_tb.v
// Project Name: 4-bit MicroProcessor prototype
// Target Device:
// Tool versions: Xilinx 13.4 ISE and lesser compatible versions
// Description: This testbench is created to test functionalities of 4-bit Microproccessor
//
// Verilog Test Fixture created by ISE for module: fa_ALU
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// Owner name : Ashik Ghona
//
////////////////////////////////////////////////////////////////////////////////
module fa_ALU_tb;
// Inputs
reg [4:0] OPCODE;
reg [3:0] A;
reg [3:0] B;
reg M;
// reg Cin;
reg CLK;
reg RESET;
wire [1:0] STATE_OUT;
wire [3:0] sum_aluout;
// Instantiate the Unit Under Test (UUT)
fa_ALU uut (
.OPCODE(OPCODE),
.A(A),
.B(B),
.M(M),
// .Cin(Cin),
.CLK(CLK),
.RESET(RESET),
.STATE_OUT(STATE_OUT),
.sum_aluout(sum_aluout)
);
initial
//To initialize clock,set required frequency by setting half cycle period (#half_cycle_time: here it's #5)
CLK=0;
always #5 CLK=~CLK;
//Instructions for arithmetic Functions
parameter DEC_A=5'b00000,
DEC_AB=5'b00010,
DEC_A_BAR_B=5'b00100,
MINUS_ONE=5'b00110,
A_PLUS_OR_A_B_BAR=5'b01000,
AB_PLUS_OR_A_B_BAR=5'b01010,
A_MINUS_B_MINUS_1=5'b01100,
A_PLUS_BAR_B=5'b01110,
A_PLUS_OR_AB=5'b10000,
A_PLUS_B=5'b10010,
A_BAR_B_PLUS_OR_AB=5'b10100,
GET_OR_AB=5'b10110,
GET_Acum=5'b11000,
AB_PLUS_A=5'b11010,
A_BAR_B_PLUS_A=5'b11100,
PASS_A=5'b11110,
GET_A=5'b00001,
GET_AB=5'b00011,
GET_A_BAR_B=5'b00101,
GET_ZERO=5'b00111,
A_PLUS_OR_A_B_BAR_PLUS_1=5'b01001,
AB_PLUS_OR_A_B_BAR_PLUS_1=5'b01011,
A_MINUS_B=5'b01101,
OR_A_BAR_B_PLUS_1=5'b01111,
A_PLUS_OR_AB_PLUS_1=5'b10001,
A_PLUS_B_PLUS_1=5'b10011,
A_BAR_B_PLUS_OR_AB_PLUS_1=5'b10101,
OR_AB_PLUS_1=5'b10111,
A_PLUS_A_PLUS_1=5'b11001,
AB_PLUS_A_PLUS_1=5'b11011,
A_BAR_B_PLUS_A_PLUS_1=5'b11101,
A_PLUS_1=5'b11111,
//Instructions for logical Functions
BAR_A=5'b0000x,
NAND_AB=5'b0001x,
BAR_A_OR_B=5'b0010x,
ONE=5'b0011x,
NOR_AB=5'b0100x,
BAR_B=5'b0101x,
XNOR_AB=5'b0110x,
A_OR_BAR_B=5'b0111x,
BAR_A_AND_B=5'b1000x,
XOR_AB=5'b1001x,
PASS_B=5'b1010x,
OR_AB=5'b1011x,
PASS_0=5'b1100x,
A_AND_BAR_B=5'b1101x,
AND_AB=5'b1110x,
OUT_A=5'b1111x;
initial
begin
// Initialize Inputs
RESET = 0;OPCODE = 0;A = 0;B = 0;
//To monitor changes in variable
$monitor("RESET=%b OPCODE=%b A=%b B=%b State=%b Result=%d",RESET,OPCODE,A,B,STATE_OUT,sum_aluout);
//arithmetic operations
#10 RESET = 1; M=0; OPCODE =GET_A; A =4'b0000; // BAR A
#30 RESET = 1; M=0; OPCODE =DEC_A; A =4'b0110; // Decreament A
#30 RESET = 1; M=0; OPCODE =DEC_AB; A =4'b1110;B =4'b1100; // Decreament AB
#30 RESET = 1; M=0; OPCODE =AB_PLUS_OR_A_B_BAR; A =4'b0100;B =4'b0001;
#30 RESET = 1; M=0; OPCODE =A_PLUS_A_PLUS_1; A =4'b0101;B =4'b0010;
#30 RESET = 1; M=0; OPCODE =MINUS_ONE; A =4'b0001; // Minus on A location
//logical operations
#30 RESET = 1;M=1; OPCODE =OR_AB; A =4'b0111;B =4'b1000; // logical OR A B
#30 RESET = 1;M=1; OPCODE =XOR_AB; A =4'b1000;B =4'b1011; // logical XOR A B
#30 RESET = 1;M=1; OPCODE =AND_AB; A =4'b1001;B =4'b0001; // logical AND A B
#30 RESET = 1;M=1; OPCODE =NAND_AB; A =4'b1010;B =4'b1010; // logical NAND A B
#30 RESET = 1;M=1; OPCODE =PASS_0; A =4'b1111; // Pass 0 on A location
#30 $stop;
end
endmodule
////////////////////////////////////////////////////////////////////////////////
// Company: Home user
// Engineer: Ashik Ghona
//
// Design Name: fa_ALU
// Module Name: D:/Data/Sandipani/Material/ASH/fa_ALU/fa_ALU_tb.v
// Project Name: 4-bit MicroProcessor prototype
// Target Device:
// Tool versions: Xilinx 13.4 ISE and lesser compatible versions
// Description: This testbench is created to test functionalities of 4-bit Microproccessor
//
// Verilog Test Fixture created by ISE for module: fa_ALU
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// Owner name : Ashik Ghona
//
////////////////////////////////////////////////////////////////////////////////
module fa_ALU_tb;
// Inputs
reg [4:0] OPCODE;
reg [3:0] A;
reg [3:0] B;
reg M;
// reg Cin;
reg CLK;
reg RESET;
wire [1:0] STATE_OUT;
wire [3:0] sum_aluout;
// Instantiate the Unit Under Test (UUT)
fa_ALU uut (
.OPCODE(OPCODE),
.A(A),
.B(B),
.M(M),
// .Cin(Cin),
.CLK(CLK),
.RESET(RESET),
.STATE_OUT(STATE_OUT),
.sum_aluout(sum_aluout)
);
initial
//To initialize clock,set required frequency by setting half cycle period (#half_cycle_time: here it's #5)
CLK=0;
always #5 CLK=~CLK;
//Instructions for arithmetic Functions
parameter DEC_A=5'b00000,
DEC_AB=5'b00010,
DEC_A_BAR_B=5'b00100,
MINUS_ONE=5'b00110,
A_PLUS_OR_A_B_BAR=5'b01000,
AB_PLUS_OR_A_B_BAR=5'b01010,
A_MINUS_B_MINUS_1=5'b01100,
A_PLUS_BAR_B=5'b01110,
A_PLUS_OR_AB=5'b10000,
A_PLUS_B=5'b10010,
A_BAR_B_PLUS_OR_AB=5'b10100,
GET_OR_AB=5'b10110,
GET_Acum=5'b11000,
AB_PLUS_A=5'b11010,
A_BAR_B_PLUS_A=5'b11100,
PASS_A=5'b11110,
GET_A=5'b00001,
GET_AB=5'b00011,
GET_A_BAR_B=5'b00101,
GET_ZERO=5'b00111,
A_PLUS_OR_A_B_BAR_PLUS_1=5'b01001,
AB_PLUS_OR_A_B_BAR_PLUS_1=5'b01011,
A_MINUS_B=5'b01101,
OR_A_BAR_B_PLUS_1=5'b01111,
A_PLUS_OR_AB_PLUS_1=5'b10001,
A_PLUS_B_PLUS_1=5'b10011,
A_BAR_B_PLUS_OR_AB_PLUS_1=5'b10101,
OR_AB_PLUS_1=5'b10111,
A_PLUS_A_PLUS_1=5'b11001,
AB_PLUS_A_PLUS_1=5'b11011,
A_BAR_B_PLUS_A_PLUS_1=5'b11101,
A_PLUS_1=5'b11111,
//Instructions for logical Functions
BAR_A=5'b0000x,
NAND_AB=5'b0001x,
BAR_A_OR_B=5'b0010x,
ONE=5'b0011x,
NOR_AB=5'b0100x,
BAR_B=5'b0101x,
XNOR_AB=5'b0110x,
A_OR_BAR_B=5'b0111x,
BAR_A_AND_B=5'b1000x,
XOR_AB=5'b1001x,
PASS_B=5'b1010x,
OR_AB=5'b1011x,
PASS_0=5'b1100x,
A_AND_BAR_B=5'b1101x,
AND_AB=5'b1110x,
OUT_A=5'b1111x;
initial
begin
// Initialize Inputs
RESET = 0;OPCODE = 0;A = 0;B = 0;
//To monitor changes in variable
$monitor("RESET=%b OPCODE=%b A=%b B=%b State=%b Result=%d",RESET,OPCODE,A,B,STATE_OUT,sum_aluout);
//arithmetic operations
#10 RESET = 1; M=0; OPCODE =GET_A; A =4'b0000; // BAR A
#30 RESET = 1; M=0; OPCODE =DEC_A; A =4'b0110; // Decreament A
#30 RESET = 1; M=0; OPCODE =DEC_AB; A =4'b1110;B =4'b1100; // Decreament AB
#30 RESET = 1; M=0; OPCODE =AB_PLUS_OR_A_B_BAR; A =4'b0100;B =4'b0001;
#30 RESET = 1; M=0; OPCODE =A_PLUS_A_PLUS_1; A =4'b0101;B =4'b0010;
#30 RESET = 1; M=0; OPCODE =MINUS_ONE; A =4'b0001; // Minus on A location
//logical operations
#30 RESET = 1;M=1; OPCODE =OR_AB; A =4'b0111;B =4'b1000; // logical OR A B
#30 RESET = 1;M=1; OPCODE =XOR_AB; A =4'b1000;B =4'b1011; // logical XOR A B
#30 RESET = 1;M=1; OPCODE =AND_AB; A =4'b1001;B =4'b0001; // logical AND A B
#30 RESET = 1;M=1; OPCODE =NAND_AB; A =4'b1010;B =4'b1010; // logical NAND A B
#30 RESET = 1;M=1; OPCODE =PASS_0; A =4'b1111; // Pass 0 on A location
#30 $stop;
end
endmodule
Saturday, March 10, 2012
4 bit Microprocessor Prototype Specifications
Kindly find the specifications pdf from my website in technical books section.
link is here :
http://akghona.webs.com/apps/documents/
//arithmetic operations
OPCODE =Add_AB; A =4'b0001;B =4'b0001; // ADD A B
OPCODE=Inc_A; A =4'b0010; // increament A
OPCODE =Trans_A; A =4'b0011; // Transfer A
OPCODE =Add_AB_Cin; A =4'b0100;B =4'b0001; // addition with carry
OPCODE =Sub_AB; A =4'b0101;B =4'b0010; // Subtraction A B
OPCODE =Dec_A; A =4'b0110; // Decreament A B
//logical operations
OPCODE =OR_AB; A =4'b0111;B =4'b1000; // logical OR A B
OPCODE =XOR_AB; A =4'b1000;B =4'b1011; // logical XOR A B
OPCODE =AND_AB; A =4'b1001;B =4'b0001; // logical AND A B
OPCODE =NOT_A; A =4'b1010; // logical NOT A
OPCODE =Sub_AB_borrow; A =4'b1111;B =4'b0010; // subtraction with borrow
link is here :
http://akghona.webs.com/apps/documents/
//arithmetic operations
OPCODE =Add_AB; A =4'b0001;B =4'b0001; // ADD A B
OPCODE=Inc_A; A =4'b0010; // increament A
OPCODE =Trans_A; A =4'b0011; // Transfer A
OPCODE =Add_AB_Cin; A =4'b0100;B =4'b0001; // addition with carry
OPCODE =Sub_AB; A =4'b0101;B =4'b0010; // Subtraction A B
OPCODE =Dec_A; A =4'b0110; // Decreament A B
//logical operations
OPCODE =OR_AB; A =4'b0111;B =4'b1000; // logical OR A B
OPCODE =XOR_AB; A =4'b1000;B =4'b1011; // logical XOR A B
OPCODE =AND_AB; A =4'b1001;B =4'b0001; // logical AND A B
OPCODE =NOT_A; A =4'b1010; // logical NOT A
OPCODE =Sub_AB_borrow; A =4'b1111;B =4'b0010; // subtraction with borrow
Testbench file for 4-bit Microprocessor
`timescale 1ns / 1ps
////////////////////////////////////////////////////////////////////////////////
// Company: Home user
// Engineer: Ashik Ghona
//
// Create Date: 22:32:58 03/07/2012
// Design Name: fa_ALU
// Module Name: D:/Data/Sandipani/Material/ASH/fa_ALU/fa_ALU_tb.v
// Project Name: 4-bit MicroProcessor prototype
// Target Device:
// Tool versions: Xilinx 13.4 ISE and lesser compatible versions
// Description: This testbench is created to test functionalities of 4-bit Microproccessor
//
// Verilog Test Fixture created by ISE for module: fa_ALU
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// Owner name : Ashik Ghona
//
////////////////////////////////////////////////////////////////////////////////
module fa_ALU_tb;
// Inputs
reg [3:0] OPCODE;
reg [3:0] A;
reg [3:0] B;
reg CLK;
reg RESET;
wire [1:0] STATE_OUT;
wire [3:0] sum_aluout;
// Instantiate the Unit Under Test (UUT)
fa_ALU uut (
.OPCODE(OPCODE),
.A(A),
.B(B),
.CLK(CLK),
.RESET(RESET),
.STATE_OUT(STATE_OUT),
.sum_aluout(sum_aluout)
);
initial
//To initialize clock,set required frequency by setting half cycle period
//(#half_cycle_time: here it's #5)
CLK=0;
always #5 CLK=~CLK;
//Instructions for arithmetic Functions
parameter Trans_A=4'b0000,
Inc_A=4'b0001,
Add_AB=4'b0010,
Add_AB_Cin=4'b0011,
Sub_AB_borrow=4'b0100,
Sub_AB=4'b0101,
Dec_A=4'b0110,
Trans_A_B1=4'b0111,
//Instructions for logical Functions
OR_AB=4'b100x,
XOR_AB=4'b101x,
AND_AB=4'b110x,
NOT_A=4'b111x;
initial
begin
// Initialize Inputs
RESET = 0;OPCODE = 0;A = 0;B = 0;
//To monitor changes in variable
$monitor("RESET=%b OPCODE=%b A=%b B=%b State=%b Result=%d",RESET,OPCODE,A,B,STATE_OUT,sum_aluout);
//arithmetic operations
#10 RESET = 1; OPCODE =Add_AB; A =4'b0001;B =4'b0001; // ADD A B
#30 RESET = 1; OPCODE =Inc_A; A =4'b0010; // increament A
#30 RESET = 1; OPCODE =Trans_A; A =4'b0011; // Transfer A
#30 RESET = 1; OPCODE =Add_AB_Cin; A =4'b0100;B =4'b0001; // addition with carry
#30 RESET = 1; OPCODE =Sub_AB; A =4'b0101;B =4'b0010; // Subtraction A B
#30 RESET = 1; OPCODE =Dec_A; A =4'b0110; // Decreament A B
//logical operations
#30 RESET = 1; OPCODE =OR_AB; A =4'b0111;B =4'b1000; // logical OR A B
#30 RESET = 1; OPCODE =XOR_AB; A =4'b1000;B =4'b1011; // logical XOR A B
#30 RESET = 1; OPCODE =AND_AB; A =4'b1001;B =4'b0001; // logical AND A B
#30 RESET = 1; OPCODE =NOT_A; A =4'b1010; // logical NOT A
#30 RESET = 1; OPCODE =Sub_AB_borrow; A =4'b1111;B =4'b0010; // subtraction with borrow
#30 $stop;
end
endmodule
////////////////////////////////////////////////////////////////////////////////
// Company: Home user
// Engineer: Ashik Ghona
//
// Create Date: 22:32:58 03/07/2012
// Design Name: fa_ALU
// Module Name: D:/Data/Sandipani/Material/ASH/fa_ALU/fa_ALU_tb.v
// Project Name: 4-bit MicroProcessor prototype
// Target Device:
// Tool versions: Xilinx 13.4 ISE and lesser compatible versions
// Description: This testbench is created to test functionalities of 4-bit Microproccessor
//
// Verilog Test Fixture created by ISE for module: fa_ALU
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// Owner name : Ashik Ghona
//
////////////////////////////////////////////////////////////////////////////////
module fa_ALU_tb;
// Inputs
reg [3:0] OPCODE;
reg [3:0] A;
reg [3:0] B;
reg CLK;
reg RESET;
wire [1:0] STATE_OUT;
wire [3:0] sum_aluout;
// Instantiate the Unit Under Test (UUT)
fa_ALU uut (
.OPCODE(OPCODE),
.A(A),
.B(B),
.CLK(CLK),
.RESET(RESET),
.STATE_OUT(STATE_OUT),
.sum_aluout(sum_aluout)
);
initial
//To initialize clock,set required frequency by setting half cycle period
//(#half_cycle_time: here it's #5)
CLK=0;
always #5 CLK=~CLK;
//Instructions for arithmetic Functions
parameter Trans_A=4'b0000,
Inc_A=4'b0001,
Add_AB=4'b0010,
Add_AB_Cin=4'b0011,
Sub_AB_borrow=4'b0100,
Sub_AB=4'b0101,
Dec_A=4'b0110,
Trans_A_B1=4'b0111,
//Instructions for logical Functions
OR_AB=4'b100x,
XOR_AB=4'b101x,
AND_AB=4'b110x,
NOT_A=4'b111x;
initial
begin
// Initialize Inputs
RESET = 0;OPCODE = 0;A = 0;B = 0;
//To monitor changes in variable
$monitor("RESET=%b OPCODE=%b A=%b B=%b State=%b Result=%d",RESET,OPCODE,A,B,STATE_OUT,sum_aluout);
//arithmetic operations
#10 RESET = 1; OPCODE =Add_AB; A =4'b0001;B =4'b0001; // ADD A B
#30 RESET = 1; OPCODE =Inc_A; A =4'b0010; // increament A
#30 RESET = 1; OPCODE =Trans_A; A =4'b0011; // Transfer A
#30 RESET = 1; OPCODE =Add_AB_Cin; A =4'b0100;B =4'b0001; // addition with carry
#30 RESET = 1; OPCODE =Sub_AB; A =4'b0101;B =4'b0010; // Subtraction A B
#30 RESET = 1; OPCODE =Dec_A; A =4'b0110; // Decreament A B
//logical operations
#30 RESET = 1; OPCODE =OR_AB; A =4'b0111;B =4'b1000; // logical OR A B
#30 RESET = 1; OPCODE =XOR_AB; A =4'b1000;B =4'b1011; // logical XOR A B
#30 RESET = 1; OPCODE =AND_AB; A =4'b1001;B =4'b0001; // logical AND A B
#30 RESET = 1; OPCODE =NOT_A; A =4'b1010; // logical NOT A
#30 RESET = 1; OPCODE =Sub_AB_borrow; A =4'b1111;B =4'b0010; // subtraction with borrow
#30 $stop;
end
endmodule
Verilog Code for 4-bit Microprocessor
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: Home user
// Engineer: Ashik Ghona
//
// Create Date: 16:47:54 07/03/2012
// Design Name: Processor functionality code
// Module Name: fa_ALU //top_module
// Project Name: 4-bit Microprocessor processor
// Target Devices:
// Tool versions: Xilinx ISE 13.4 and lesser compatible versions
// Description: This code is created to write functionalities of 4-bit microprocessors.
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
//Module for XOR_Gate/////////////////////////////////////////////////////////////////
module xor_gate(a,b,y);
input a,b;
output y;
reg y;
always@(a,b)
begin
if (a==b)
y=0;
else
y=1;
end
endmodule
////////////////////////////////////////////////////////////////////////////////
//Module for 4bit mux41_4 having 4bit_bus I/O///////////////////////////////////
module mux41_4(input [3:0] i1,i2,i3,i4 ,input s1,s0,output reg [3:0] out);
always@(i1,i2,i3,i4,i1,s0,s1) //s1,s0 for select line, i1,i2,i3,i4 inputs
begin
case({s1,s0})
2'b00 : out=i1;
2'b01 : out=i2;
2'b10 : out=i3;
2'b11 : out=i4;
endcase
end
endmodule
/////////////////////////////////////////////////////////////////////////////////
//Module for 4bit bus mux21_4 having 4bit_bus I/O////////////////////////////////
module mux21_4(input [3:0] i1,i2,input s2,output reg [3:0] out);
always@(s2,i1,i2) //s2 for select line, i1,i2 inputs
begin
case(s2)
1'b0 : out=i1;
1'b1 : out=i2;
endcase
end
endmodule
/////////////////////////////////////////////////////////////////////////////////
//Module for 1bit Full adder/////////////////////////////////////////////////////
module fa_1_bit(a,b,cin,s,cout);
input a,b;
input cin;
output s,cout;
wire w1;
xor_gate xor1(a,b,w1); //Instantiation from xor_gate module
xor_gate xor2(w1,cin,s); //Instantiation from xor_gate module
and and1(o1,w1,cin);
and and2(o2,a,b);
or or1(cout,o1,o2);
endmodule
/////////////////////////////////////////////////////////////////////////////////
//Module for 1bit Full adder/////////////////////////////////////////////////////
module alu_4bit(a,b,cin,sel,cout,temp_result);
input [3:0] a,b;
input [2:0] sel;
input cin;
output [3:0] cout,temp_result;
wire [3:0] bb;
wire [3:0] tsl1,tsl2,tsl3,tsl4,tl,sum;
//Block for arithmetic operations
//Instantiation from mux41_4 having 4bit_bus I/O
mux41_4 mux41_arithmetic(4'b0000,b,~b,4'b1111,sel[1],sel[0],bb);
//bb is the output of mux_41_arithmetic
//Instantiation from fa_1_bit module
fa_1_bit fadder1(a[0],bb[0],cin,sum[0],cout[0]);
fa_1_bit fadder2(a[1],bb[1],cout[0],sum[1],cout[1]);
fa_1_bit fadder3(a[2],bb[2],cout[1],sum[2],cout[2]);
fa_1_bit fadder4(a[3],bb[3],cout[2],sum[3],cout[3]);
//s[3:1] is the output of this 4bit adder block
//Block for logical operation
assign tsl1=a|b;
assign tsl2=a^b;
assign tsl3=a&b;
assign tsl4=~a;
//Instantiation from mux41_4 having 4bit_bus I/O
mux41_4 mux41_logical(tsl1,tsl2,tsl3,tsl4,sel[1],sel[0],tl);
//tl is the output of mux41_logical
//Instantiation from mux21_4 having 4bit_bus I/O
mux21_4 alu_output(sum,tl,sel[2],temp_result); //temp_result is the result of alu_output
endmodule
//////////////////////////////////////////////////////////////////////////////
//Module 4-bit Registered ALU/////////////////////////////////////////////////
module reg_alu_top_module(a,b,cin,sel,sum,clk,rst);
input [3:0] a,b;
input [2:0] sel;
input cin,clk;
input rst;
output reg [3:0] sum;
wire [3:0] result;
wire [3:0] cout;
alu_4bit alu(a,b,cin,sel,cout,result);
always@(posedge clk or negedge rst)
if(!rst)
sum<=4'b0000;
else
begin
sum<=result; //sum is the result of reg_alu_top_module
end
endmodule
///////////////////////////////////////////////////////////////////////////////
//Instruction Decoder Module//////////////////////////////////////////////////
// This module is the brain of the processor.
// It is implemented using Moore model FSM.
module instr_decoder_fsm(
input clk, rst,cin,
input [3:0] oper1,oper2,datain,aluout,
input [2:0] opcode,
output reg [3:0] address,aluin1,aluin2,dataout,
output reg [2:0] selalu,
output reg rwn,chipsel,
output reg [1:0] state,
output reg cin_out
);
reg [1:0] current_state;
reg [1:0] next_state;
parameter idle = 2'b00,
fetch = 2'b01,
execute = 2'b10,
load = 2'b11;
// always block for State transition logic
always @(posedge clk or negedge rst)
if (!rst)
current_state <= idle;
else
current_state <= next_state;
// always block Next state logic
always @(current_state)
begin
next_state = 2'bx;
case(current_state)
idle: next_state = fetch;
fetch: next_state = execute ;
execute: next_state = load;
load: next_state = fetch;
endcase
end
//always for Output logic
always @(current_state,aluout)
begin
case (current_state)
idle:
begin
state=idle;
chipsel=1'b1;
rwn=1'bx;
address=oper1;
aluin1=4'b0000;
aluin2=4'b0000;
cin_out=1'b0;
dataout=4'b0000;
selalu=3'b000;
end
fetch:
begin
state=fetch;
chipsel=1'b0;
rwn=1'b1; //rwn=1 for reading memory
address=oper1; // put the address(oper1) to read data from that memory location of RAM
dataout=4'bzzzz;
aluin1=datain;
aluin2=oper2;
cin_out=cin;
selalu=opcode;
end
execute:
begin
state=execute;
selalu=opcode; //Giving opcode for operations to ALU
address=oper1;
aluin1=datain; //Giving datain(which was read fro RAM in previous cycle) to ALU
aluin2=oper2; // Giving oper2 to ALU
cin_out=cin; //Giving cin to ALU.
chipsel=1'b0;
rwn=1'bz;
dataout=4'b0000;
end
load:
begin
state=load;
address=oper1; //Put the address(oper1) for RAM locaton
dataout=aluout; //put the ALU output on dataout
rwn=1'b0; //Writing ALU output to RAM
chipsel=1'b0; //Selecting RAM
selalu=3'bzzz;
aluin1=4'bzzzz;
aluin2=4'bzzzz;
cin_out=1'bz;
end
endcase
end
endmodule
///////////////////////////////////////////////////////////////////////////////
//Data_RAM module//////////////////////////////////////////////////////////////
module dataram(dataout, addr, datain, csn, rwn);
input [3:0] addr;
input [3:0] datain;
input csn;
input rwn;
output reg [3:0] dataout;
reg [3:0] data [15:0];
always@(rwn,datain,csn,addr)
begin
if(csn == 1)
begin
data[0] = 0;
data[1] = 1;
data[2] = 2;
data[3] = 3;
data[4] = 4;
data[5] = 5;
data[6] = 6;
data[7] = 7;
data[8] = 8;
data[9] = 9;
data[10] = 10;
data[11] = 11;
data[12] = 12;
data[13] = 13;
data[14] = 14;
data[15] = 15;
end
if(csn==0 && rwn==1)
begin
dataout = data[addr];
end
else if(csn==0 && rwn==0)
begin
data[addr] = datain;
dataout = 4'bzzzz;
end
else
begin
dataout = 4'bzzzz;
end
end
endmodule
//////////////////////////////////////////////////////////////////////////////////
//Top module for 4-bit Microprocessor/////////////////////////////////////////////
module fa_ALU(
input [3:0] OPCODE,
input [3:0] A,
input [3:0] B,
input CLK,
input RESET,
output [1:0] STATE_OUT,
output [3:0] sum_aluout
);
//wire [1:0] STATE_OUT;
//wire [3:0] sum_aluout;
/*here I'm taking STATE_OUT and sum_aluout as output just to monitor results and state of FSM*/
/*If you don't want to monitor them then just remove them from port list and declare them wire*/
wire [3:0] ram_out_deco_in,address,aluin1_A, aluin2_B, deco_out_ram_in;
wire [2:0] selalu_sel;
wire rwn,chipsel,cout_cin;
//Instantiation from Instr_decoder_fsm module
instr_decoder_fsm decoder(
CLK,RESET,OPCODE[0],A,B,ram_out_deco_in,sum_aluout,OPCODE[3:1],
address,aluin1_A,aluin2_B,deco_out_ram_in,selalu_sel,rwn,
chipsel,STATE_OUT,cout_cin);
//Instantiation from fa4bit_alu_final
reg_alu_top_module reg_alu(
aluin1_A,aluin2_B,cout_cin,selalu_sel,
sum_aluout,CLK,RESET);
//Instantiation from distram module
dataram ram(
ram_out_deco_in, address, deco_out_ram_in,chipsel,rwn);
endmodule
/////////////////////////////////////////////////////////////////////////////////////////////
// Code OVER
//////////////////////////////////////////////////////////////////////////////////
// Company: Home user
// Engineer: Ashik Ghona
//
// Create Date: 16:47:54 07/03/2012
// Design Name: Processor functionality code
// Module Name: fa_ALU //top_module
// Project Name: 4-bit Microprocessor processor
// Target Devices:
// Tool versions: Xilinx ISE 13.4 and lesser compatible versions
// Description: This code is created to write functionalities of 4-bit microprocessors.
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
//Module for XOR_Gate/////////////////////////////////////////////////////////////////
module xor_gate(a,b,y);
input a,b;
output y;
reg y;
always@(a,b)
begin
if (a==b)
y=0;
else
y=1;
end
endmodule
////////////////////////////////////////////////////////////////////////////////
//Module for 4bit mux41_4 having 4bit_bus I/O///////////////////////////////////
module mux41_4(input [3:0] i1,i2,i3,i4 ,input s1,s0,output reg [3:0] out);
always@(i1,i2,i3,i4,i1,s0,s1) //s1,s0 for select line, i1,i2,i3,i4 inputs
begin
case({s1,s0})
2'b00 : out=i1;
2'b01 : out=i2;
2'b10 : out=i3;
2'b11 : out=i4;
endcase
end
endmodule
/////////////////////////////////////////////////////////////////////////////////
//Module for 4bit bus mux21_4 having 4bit_bus I/O////////////////////////////////
module mux21_4(input [3:0] i1,i2,input s2,output reg [3:0] out);
always@(s2,i1,i2) //s2 for select line, i1,i2 inputs
begin
case(s2)
1'b0 : out=i1;
1'b1 : out=i2;
endcase
end
endmodule
/////////////////////////////////////////////////////////////////////////////////
//Module for 1bit Full adder/////////////////////////////////////////////////////
module fa_1_bit(a,b,cin,s,cout);
input a,b;
input cin;
output s,cout;
wire w1;
xor_gate xor1(a,b,w1); //Instantiation from xor_gate module
xor_gate xor2(w1,cin,s); //Instantiation from xor_gate module
and and1(o1,w1,cin);
and and2(o2,a,b);
or or1(cout,o1,o2);
endmodule
/////////////////////////////////////////////////////////////////////////////////
//Module for 1bit Full adder/////////////////////////////////////////////////////
module alu_4bit(a,b,cin,sel,cout,temp_result);
input [3:0] a,b;
input [2:0] sel;
input cin;
output [3:0] cout,temp_result;
wire [3:0] bb;
wire [3:0] tsl1,tsl2,tsl3,tsl4,tl,sum;
//Block for arithmetic operations
//Instantiation from mux41_4 having 4bit_bus I/O
mux41_4 mux41_arithmetic(4'b0000,b,~b,4'b1111,sel[1],sel[0],bb);
//bb is the output of mux_41_arithmetic
//Instantiation from fa_1_bit module
fa_1_bit fadder1(a[0],bb[0],cin,sum[0],cout[0]);
fa_1_bit fadder2(a[1],bb[1],cout[0],sum[1],cout[1]);
fa_1_bit fadder3(a[2],bb[2],cout[1],sum[2],cout[2]);
fa_1_bit fadder4(a[3],bb[3],cout[2],sum[3],cout[3]);
//s[3:1] is the output of this 4bit adder block
//Block for logical operation
assign tsl1=a|b;
assign tsl2=a^b;
assign tsl3=a&b;
assign tsl4=~a;
//Instantiation from mux41_4 having 4bit_bus I/O
mux41_4 mux41_logical(tsl1,tsl2,tsl3,tsl4,sel[1],sel[0],tl);
//tl is the output of mux41_logical
//Instantiation from mux21_4 having 4bit_bus I/O
mux21_4 alu_output(sum,tl,sel[2],temp_result); //temp_result is the result of alu_output
endmodule
//////////////////////////////////////////////////////////////////////////////
//Module 4-bit Registered ALU/////////////////////////////////////////////////
module reg_alu_top_module(a,b,cin,sel,sum,clk,rst);
input [3:0] a,b;
input [2:0] sel;
input cin,clk;
input rst;
output reg [3:0] sum;
wire [3:0] result;
wire [3:0] cout;
alu_4bit alu(a,b,cin,sel,cout,result);
always@(posedge clk or negedge rst)
if(!rst)
sum<=4'b0000;
else
begin
sum<=result; //sum is the result of reg_alu_top_module
end
endmodule
///////////////////////////////////////////////////////////////////////////////
//Instruction Decoder Module//////////////////////////////////////////////////
// This module is the brain of the processor.
// It is implemented using Moore model FSM.
module instr_decoder_fsm(
input clk, rst,cin,
input [3:0] oper1,oper2,datain,aluout,
input [2:0] opcode,
output reg [3:0] address,aluin1,aluin2,dataout,
output reg [2:0] selalu,
output reg rwn,chipsel,
output reg [1:0] state,
output reg cin_out
);
reg [1:0] current_state;
reg [1:0] next_state;
parameter idle = 2'b00,
fetch = 2'b01,
execute = 2'b10,
load = 2'b11;
// always block for State transition logic
always @(posedge clk or negedge rst)
if (!rst)
current_state <= idle;
else
current_state <= next_state;
// always block Next state logic
always @(current_state)
begin
next_state = 2'bx;
case(current_state)
idle: next_state = fetch;
fetch: next_state = execute ;
execute: next_state = load;
load: next_state = fetch;
endcase
end
//always for Output logic
always @(current_state,aluout)
begin
case (current_state)
idle:
begin
state=idle;
chipsel=1'b1;
rwn=1'bx;
address=oper1;
aluin1=4'b0000;
aluin2=4'b0000;
cin_out=1'b0;
dataout=4'b0000;
selalu=3'b000;
end
fetch:
begin
state=fetch;
chipsel=1'b0;
rwn=1'b1; //rwn=1 for reading memory
address=oper1; // put the address(oper1) to read data from that memory location of RAM
dataout=4'bzzzz;
aluin1=datain;
aluin2=oper2;
cin_out=cin;
selalu=opcode;
end
execute:
begin
state=execute;
selalu=opcode; //Giving opcode for operations to ALU
address=oper1;
aluin1=datain; //Giving datain(which was read fro RAM in previous cycle) to ALU
aluin2=oper2; // Giving oper2 to ALU
cin_out=cin; //Giving cin to ALU.
chipsel=1'b0;
rwn=1'bz;
dataout=4'b0000;
end
load:
begin
state=load;
address=oper1; //Put the address(oper1) for RAM locaton
dataout=aluout; //put the ALU output on dataout
rwn=1'b0; //Writing ALU output to RAM
chipsel=1'b0; //Selecting RAM
selalu=3'bzzz;
aluin1=4'bzzzz;
aluin2=4'bzzzz;
cin_out=1'bz;
end
endcase
end
endmodule
///////////////////////////////////////////////////////////////////////////////
//Data_RAM module//////////////////////////////////////////////////////////////
module dataram(dataout, addr, datain, csn, rwn);
input [3:0] addr;
input [3:0] datain;
input csn;
input rwn;
output reg [3:0] dataout;
reg [3:0] data [15:0];
always@(rwn,datain,csn,addr)
begin
if(csn == 1)
begin
data[0] = 0;
data[1] = 1;
data[2] = 2;
data[3] = 3;
data[4] = 4;
data[5] = 5;
data[6] = 6;
data[7] = 7;
data[8] = 8;
data[9] = 9;
data[10] = 10;
data[11] = 11;
data[12] = 12;
data[13] = 13;
data[14] = 14;
data[15] = 15;
end
if(csn==0 && rwn==1)
begin
dataout = data[addr];
end
else if(csn==0 && rwn==0)
begin
data[addr] = datain;
dataout = 4'bzzzz;
end
else
begin
dataout = 4'bzzzz;
end
end
endmodule
//////////////////////////////////////////////////////////////////////////////////
//Top module for 4-bit Microprocessor/////////////////////////////////////////////
module fa_ALU(
input [3:0] OPCODE,
input [3:0] A,
input [3:0] B,
input CLK,
input RESET,
output [1:0] STATE_OUT,
output [3:0] sum_aluout
);
//wire [1:0] STATE_OUT;
//wire [3:0] sum_aluout;
/*here I'm taking STATE_OUT and sum_aluout as output just to monitor results and state of FSM*/
/*If you don't want to monitor them then just remove them from port list and declare them wire*/
wire [3:0] ram_out_deco_in,address,aluin1_A, aluin2_B, deco_out_ram_in;
wire [2:0] selalu_sel;
wire rwn,chipsel,cout_cin;
//Instantiation from Instr_decoder_fsm module
instr_decoder_fsm decoder(
CLK,RESET,OPCODE[0],A,B,ram_out_deco_in,sum_aluout,OPCODE[3:1],
address,aluin1_A,aluin2_B,deco_out_ram_in,selalu_sel,rwn,
chipsel,STATE_OUT,cout_cin);
//Instantiation from fa4bit_alu_final
reg_alu_top_module reg_alu(
aluin1_A,aluin2_B,cout_cin,selalu_sel,
sum_aluout,CLK,RESET);
//Instantiation from distram module
dataram ram(
ram_out_deco_in, address, deco_out_ram_in,chipsel,rwn);
endmodule
/////////////////////////////////////////////////////////////////////////////////////////////
// Code OVER
Sunday, January 8, 2012
You should Try it...Seriously...!!
Before you can grow up, you must fall in love three times...!!
Once, you must fall in love with your best friend, ruining your friendship forever. This will teach you who your true friends are, and the fine line between friendship and more.
Once you must fall in love with someone you believe is perfect. You will learn that no one is perfect, and that you should never be treated as anything less than you deserve.
And once, you must fall in love with someone that is exactly like you. This will teach you about who you are, and who you want to be.
And when you’re through with all that, you learn that the people who care about you the most are the ones that you hurt, and the ones that hurt you are the ones that needed you the most.
But most of all, you learn that love is only a concept and is not something that can be defined, it is different to each and every person on this earth, knowing that everyone wants to be loved.....!!
Once, you must fall in love with your best friend, ruining your friendship forever. This will teach you who your true friends are, and the fine line between friendship and more.
Once you must fall in love with someone you believe is perfect. You will learn that no one is perfect, and that you should never be treated as anything less than you deserve.
And once, you must fall in love with someone that is exactly like you. This will teach you about who you are, and who you want to be.
And when you’re through with all that, you learn that the people who care about you the most are the ones that you hurt, and the ones that hurt you are the ones that needed you the most.
But most of all, you learn that love is only a concept and is not something that can be defined, it is different to each and every person on this earth, knowing that everyone wants to be loved.....!!
Subscribe to:
Posts (Atom)


