module control(clk,rst,empty,opcode,da,in_reg,rd,wr,fetch,alu_ena,ir_ena,da_ena,ld_da_x,ld_ir_ptr,load_ptr,
load_pc,inc_pc,inc_ptr,dec_ptr,datactl_ena,ld_a,ld_b,halt,stop,dr_o,ld_in,iram_sel,dram_sel,ld_s1,s);
input clk,rst,empty;
input [7:0]opcode,da,in_reg;
output rd,wr,fetch,alu_ena,ir_ena,da_ena,ld_da_x,ld_ir_ptr,load_ptr,load_pc,inc_ptr,dec_ptr,
inc_pc,datactl_ena,ld_a,ld_b,halt,stop,dr_o,ld_in,iram_sel,dram_sel,ld_s1;
output [2:0]s;
reg rd,wr,fetch,alu_ena,ir_ena,da_ena,ld_da_x,ld_ir_ptr,load_ptr,load_pc,inc_ptr,dec_ptr,inc_pc,
datactl_ena,ld_a,ld_b,halt,stop,dr_o,ld_in,iram_sel,dram_sel,ld_s1;
reg [2:0]s;
reg [3:0]state;
parameter LDA=8'b0000_0001, ADD=8'b0000_0000, SUB=8'b0000_0010, OUT=8'b0000_0011, JMP=8'b0000_0100, JZ=8'b0000_0101, JN=8'b0000_0110,
DATX=8'b0000_0111, IN=8'b0000_1000, STR=8'b0000_1001, SDAN=8'b0000_1010, STPK=8'b0000_1011, JK=8'b0000_1100, XTDA=8'b0000_1101,
INC=8'b00001_110, DEC=8'b00001_111, ZERO=8'b00010_000, INP=8'b00010_001, STRP=8'b00010_010, JEND=8'b00010_011, LNOT=8'b00010_100,
LAND=8'b00010_101, LOR=8'b00010_110, MULT=8'b00010_111, DIVI=8'b00011_000, STP=8'b00011_001;
parameter CLK1=4'b0000, CLK2=4'b0001, CLK3=4'b0010, CLK4=4'b0011,
CLK5=4'b0100, CLK6=4'b0101, CLK7=4'b0110, CLK8=4'b0111;
always @(posedge clk or posedge rst)
if(rst) state<=4'b0000;
else
begin
{rd,wr,fetch,alu_ena,ir_ena,da_ena,ld_da_x,ld_ir_ptr,load_ptr,load_pc,inc_ptr,dec_ptr,
inc_pc,datactl_ena,ld_a,ld_b,halt,stop,dr_o,ld_in,iram_sel,dram_sel,ld_s1,s}<=26'b0;
case(state)
CLK1: begin
fetch<=1; rd<=1; iram_sel<=1; ir_ena<=1; state<=CLK2;
end
CLK2: begin
fetch<=1; rd<=1; iram_sel<=1; ir_ena<=1; inc_pc<=1; state<=CLK3;
end
CLK3: case(opcode)
LDA: state<=CLK4; ADD: state<=CLK4; SUB: state<=CLK4; OUT: state<=CLK4; IN: state<=CLK4; STR: state<=CLK4;
SDAN: state<=CLK4; LAND: state<=CLK4; LOR: state<=CLK4; MULT: state<=CLK4; DIVI: state<=CLK4; JZ: state<=CLK4;
JN: state<=CLK4; JMP: state<=CLK4; JK: state<=CLK4; JEND: state<=CLK4;
INC: begin
inc_ptr<=1; state<=CLK4;
end
DEC: begin
dec_ptr<=1; state<=CLK4;
end
ZERO: begin
s<=3'b000; da_ena<=1; state<=CLK4;
end
INP: begin
ld_ir_ptr<=1; state<=CLK4;
end
STRP: begin
datactl_ena<=1; wr<=1; dram_sel<=1; load_ptr<=1; state<=CLK4;
end
LNOT: begin
ld_a<=1; state<=CLK4;
end
STPK:
begin
halt<=1; state<=CLK4;
end
DATX: begin
ld_da_x<=1; state<=CLK4;
end
XTDA: begin
s<=3'b010; da_ena<=1; state<=CLK4;
end
STP: begin
stop<=1; state<=CLK1;
end
default:
begin
state<=CLK4;
{rd,wr,fetch,alu_ena,ir_ena,da_ena,ld_da_x,ld_ir_ptr,load_ptr,load_pc,inc_ptr,dec_ptr,
inc_pc,datactl_ena,ld_a,ld_b,halt,stop,dr_o,ld_in,iram_sel,dram_sel,ld_s1,s}<=26'b0;
end
endcase
CLK4: begin
inc_pc<=1;
case(opcode)
LDA: state<=CLK5; ADD: state<=CLK5; SUB: state<=CLK5; OUT: state<=CLK5; IN: state<=CLK5; STR: state<=CLK5;
LAND: state<=CLK5; LOR: state<=CLK5; MULT: state<=CLK5; DIVI: state<=CLK5; INP: state<=CLK5; STP: state<=CLK5;
INC: state<=CLK1; DEC: state<=CLK1; STRP: state<=CLK1; DATX: state<=CLK1; XTDA: state<=CLK1; ZERO: state<=CLK1;
JMP:
begin
load_pc<=1; state<=CLK1;
end
JZ:
if(da==8'b0)
begin
load_pc<=1; state<=CLK1;
end
else state<=CLK1;
JN:
if(da<0)
begin
load_pc<=1; state<=CLK1;
end
else state<=CLK1;
JEND:
if(in_reg==8'h80)
begin
load_pc<=1; state<=CLK1;
end
else state<=CLK1;
SDAN:
begin
s<=3'b100; da_ena<=1; state<=CLK1;
end
LNOT:
begin
alu_ena<=1; s<=3'b001; da_ena<=1; state<=CLK1;
end
STPK:
state<=CLK5;
JK:
if(empty==1)
begin
load_pc<=1; state<=CLK1;
end
else state<=CLK1;
default:
begin
state<=CLK5;
{rd,wr,fetch,alu_ena,ir_ena,da_ena,ld_da_x,ld_ir_ptr,load_ptr,load_pc,inc_ptr,dec_ptr,
inc_pc,datactl_ena,ld_a,ld_b,halt,stop,dr_o,ld_in,iram_sel,dram_sel,ld_s1,s}<=26'b0;
end
endcase
end
CLK5:
begin
case(opcode)
LDA:
begin
rd<=1; dram_sel<=1; s<=3'b011; da_ena<=1; state<=CLK1;
end
ADD:
begin
ld_b<=1; state<=CLK6;
end
SUB:
begin
ld_b<=1; state<=CLK6;
end
OUT:
begin
rd<=1; dram_sel<=1; dr_o<=1; state<=CLK1;
end
IN:
begin
ld_in<=1; wr<=1; dram_sel<=1; state<=CLK1;
end
STR:
begin
datactl_ena<=1; wr<=1; dram_sel<=1; state<=CLK1;
end
INP:
begin
ld_in<=1; wr<=1; state<=CLK1;
end
LAND:
begin
ld_b<=1; state<=CLK6;
end
LOR:
begin
ld_b<=1; state<=CLK6;
end
MULT:
begin
ld_b<=1; state<=CLK6;
end
DIVI:
begin
ld_b<=1; state<=CLK6;
end
JMP: state<=CLK1; JZ: state<=CLK1; JN: state<=CLK1; STR: state<=CLK1; SDAN: state<=CLK1; INC: state<=CLK1;
DEC: state<=CLK1; ZERO: state<=CLK1; JEND: state<=CLK1; LNOT: state<=CLK1; JK: state<=CLK1; DATX: state<=CLK1;
XTDA: state<=CLK1; STP: state<=CLK1; STPK: state<=CLK6;
default:
begin
state<=CLK6;
{rd,wr,fetch,alu_ena,ir_ena,da_ena,ld_da_x,ld_ir_ptr,load_ptr,load_pc,inc_ptr,dec_ptr,
inc_pc,datactl_ena,ld_a,ld_b,halt,stop,dr_o,ld_in,iram_sel,dram_sel,ld_s1,s}<=26'b0;
end
endcase
end
CLK6:
begin
case(opcode)
ADD:
begin
ld_a<=1; state<=CLK7;
end
SUB:
begin
ld_a<=1; state<=CLK7;
end
LAND:
begin
ld_a<=1; state<=CLK7;
end
LOR:
begin
ld_a<=1; state<=CLK7;
end
MULT:
begin
ld_a<=1; state<=CLK7;
end
DIVI:
begin
ld_a<=1; state<=CLK7;
end
STPK: state<=CLK7;
JMP: state<=CLK1; JZ: state<=CLK1; JN: state<=CLK1; STR: state<=CLK1; SDAN: state<=CLK1; INC: state<=CLK1; IN: state<=CLK1;
DEC: state<=CLK1; ZERO: state<=CLK1; JEND: state<=CLK1; LNOT: state<=CLK1; JK: state<=CLK1; DATX: state<=CLK1;
XTDA: state<=CLK1; STP: state<=CLK1; INP: state<=CLK1; STR: state<=CLK1; OUT: state<=CLK1; LDA: state<=CLK1;
default:
begin
state<=CLK7;
{rd,wr,fetch,alu_ena,ir_ena,da_ena,ld_da_x,ld_ir_ptr,load_ptr,load_pc,inc_ptr,dec_ptr,
inc_pc,datactl_ena,ld_a,ld_b,halt,stop,dr_o,ld_in,iram_sel,dram_sel,ld_s1,s}<=26'b0;
end
endcase
end
CLK7: begin
case(opcode)
ADD:
begin
alu_ena<=1; s<=3'b001; da_ena<=1; state<=CLK1;
end
SUB:
begin
alu_ena<=1; s<=3'b001; da_ena<=1; state<=CLK1;
end
LAND:
begin
alu_ena<=1; s<=3'b001; da_ena<=1; state<=CLK1;
end
LOR:
begin
alu_ena<=1; s<=3'b001; da_ena<=1; state<=CLK1;
end
MULT:
begin
alu_ena<=1; s<=3'b001; da_ena<=1; state<=CLK8;
end
DIVI:
begin
alu_ena<=1; s<=3'b001; da_ena<=1; state<=CLK8;
end
STPK: state<=CLK8;
JMP: state<=CLK1; JZ: state<=CLK1; JN: state<=CLK1; STR: state<=CLK1; SDAN: state<=CLK1; INC: state<=CLK1; IN: state<=CLK1;
DEC: state<=CLK1; ZERO: state<=CLK1; JEND: state<=CLK1; LNOT: state<=CLK1; JK: state<=CLK1; DATX: state<=CLK1;
XTDA: state<=CLK1; STP: state<=CLK1; INP: state<=CLK1; STR: state<=CLK1; OUT: state<=CLK1; LDA: state<=CLK1;
default:
begin
state<=CLK8;
{rd,wr,fetch,alu_ena,ir_ena,da_ena,ld_da_x,ld_ir_ptr,load_ptr,load_pc,inc_ptr,dec_ptr,
inc_pc,datactl_ena,ld_a,ld_b,halt,stop,dr_o,ld_in,iram_sel,dram_sel,ld_s1,s}<=26'b0;
end
endcase
end
CLK8:
begin
case(opcode)
MULT:
begin
ld_s1<=1; state<=CLK1;
end
DIVI:
begin
ld_s1<=1; state<=CLK1;
end
JMP: state<=CLK1; JZ: state<=CLK1; JN: state<=CLK1; STR: state<=CLK1; SDAN: state<=CLK1; INC: state<=CLK1;
DEC: state<=CLK1; ZERO: state<=CLK1; JEND: state<=CLK1; LNOT: state<=CLK1; JK: state<=CLK1; DATX: state<=CLK1;
XTDA: state<=CLK1; STP: state<=CLK1; INP: state<=CLK1; STRP: state<=CLK1; OUT: state<=CLK1; LDA: state<=CLK1;
IN: state<=CLK1; ADD: state<=CLK1; SUB: state<=CLK1; LAND: state<=CLK1; LOR: state<=CLK1; STPK: state<=CLK1;
default:
begin
state<=CLK1;
{rd,wr,fetch,alu_ena,ir_ena,da_ena,ld_da_x,ld_ir_ptr,load_ptr,load_pc,inc_ptr,dec_ptr,
inc_pc,datactl_ena,ld_a,ld_b,halt,stop,dr_o,ld_in,iram_sel,dram_sel,ld_s1,s}<=26'b0;
end
endcase
end
default: begin
{rd,wr,fetch,alu_ena,ir_ena,da_ena,ld_da_x,ld_ir_ptr,load_ptr,load_pc,inc_ptr,dec_ptr,
inc_pc,datactl_ena,ld_a,ld_b,halt,stop,dr_o,ld_in,iram_sel,dram_sel,ld_s1,s}<=26'b0;
state<=CLK1;
end
endcase
end
endmodule
此帖出自
小平头技术问答
一周热门 更多>