《简化的RISCCPU设计方案.ppt》由会员分享,可在线阅读,更多相关《简化的RISCCPU设计方案.ppt(75页珍藏版)》请在三一办公上搜索。
1、第17章 简化的RISC_CPU设计,精简指令集(RISC)CPU的构造原理和设计方法,17.1 什么是CPU,CPU:中央处理单元,是计算机的核心部件,计算机进行信息处理分两个步骤:将数据和程序(指令序列)输入到计算机的存储器中从第一条指令的地址起开始执行程序,得到所需结果,结束运行。,CPU的作用:协调并控制计算机的各部件,使之有条不紊的执行程序,CPU的基本功能:取指令-地址与控制信号分析指令-即指令译码,操作和操作控制信号执行指令-操作控制信号作用于各部件,CPU的基本功能概括:能对指令进行译码并执行规定的动作可以进行算术和逻辑运算能与存储器和外设交换数据提供系统所需的控制,CPU的内
2、部结构:算术逻辑运算单元(ALU)累加器程序计数器指令寄存器和译码器时序和控制部件,RISC即精简指令集计算机:Reduced Instruction Set Computer时序控制信号由硬件而不是微程序控制,cpu,AB,DB,地址译码器,ROM,RAM,clock,reset,MICRO COMPUTER SYSTEM,read,write,D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0,高8位,低8位,地址码,指令码,指令,指令数目:8条地址空间:213=8K,助记符 操作码 功能HLT 3b000 暂停SKZ 3b001 累
3、加器为零转移ADD 3b010 加ANDD 3b011 与XORR 3b100 或LDA 3b101 取数STO 3b110 存数JMP 3b111 转移,指令集,CPU的基本功能:取指令-地址与控制信号分析指令-即指令译码,操作和操作控制信号执行指令-操作控制信号作用于各部件,CPU的基本功能概括:能对指令进行译码并执行规定的动作可以进行算术和逻辑运算能与存储器和外设交换数据提供系统所需的控制,CPU的内部结构:算术逻辑运算单元(ALU)累加器程序计数器指令寄存器和译码器时序和控制部件,累加器A,指令寄存器IR,算逻单元ALU,数据控制,地址选择,程序计数器PC,控制状态机,时钟信号,CPU
4、的结构功能图,累加器A,指令寄存器IR,算逻单元ALU,数据控制,地址选择,程序计数器PC,控制状态机,时钟信号,AB,DB,halt,rd,wr,clk,rst,load_acc,fetch,inc_pc,load_pc,load_ir,alu_ena,datactl_ena,halt,load_ir,rd,inc_pc,alu_ena,fetch,clk,0,1,2,3,4,5,6,7,halt指令,data,load_ir,rd,inc_pc,alu_ena,fetch,clk,0,1,2,3,4,5,6,7,LDA 1000,B0,00,EE,load_acc,address,0000
5、,0001,1000,0002,data,load_ir,rd,inc_pc,alu_ena,fetch,clk,0,1,2,3,4,5,6,7,ADD 1001,50,01,11,load_acc,address,0002,0003,1001,0004,data,load_ir,rd,inc_pc,alu_ena,fetch,clk,0,1,2,3,4,5,6,7,STO 1001,D0,01,FF,wr,address,0004,0005,1001,0006,17.3.1 时钟发生器,fetch是控制信号,clk的8分频信号alu_ena用于控制算术逻辑运算单元clk是时钟信号reset是
6、复位信号,状态转移图,17.3.1 时钟发生器,S2,S1,S2,S3,S4,S5,S6,S7,S8,S1,S2,S3,S4,S5,S6,S7,S8,idle,/-clk_gen.v-timescale 1ns/1nsmodule clk_gen(clk,reset,fetch,alu_ena);input clk,reset;output fetch,alu_ena;wire clk,reset;reg fetch,alu_ena;reg 7:0state;parameter S1=8b00000001,S2=8b00000010,S3=8b00000100,S4=8b00001000,S5
7、=8b00010000,S6=8b00100000,S7=8b01000000,S8=8b10000000,idle=8b00000000;,17.3.1 时钟发生器,always(posedge clk)if(reset)begin fetch=0;alu_ena=0;state=idle;end else begin case(state)default:state=idle;endcase end,case(state)S1:begin fetch=1;state=S2;end S2:state=S3;S3:state=S4;S4:state=S5;S5:begin state=S6;f
8、etch=0;end S6:begin state=S7;alu_ena=1;end S7:begin alu_ena=0;state=S8;end S8:state=S1;idle:state=S1;default:state=idle;endcase,17.3.2 指令寄存器,reset,clk,load_ir,opcode2:0,ir_addr12:0,timescale 1ns/1nsmodule register(opc_iraddr,data,ena,clk,rst);output 15:0opc_iraddr;input 7:0data;input ena,clk,rst;reg
9、 15:0opc_iraddr;reg state;always(posedge clk)begin if(rst)begin opc_iraddr=16b0000_0000_0000_0000;state=1b0;end else,if(ena)casex(state)1b0:begin opc_iraddr15:8=data;state=1;end 1b1:begin opc_iraddr7:0=data;state=0;end default:begin opc_iraddr15:0=16bx;state=1bx;end endcaseelse state=1b0;end endmodu
10、le,17.3.3 累加器,reset,clk,load_acc,alu_out7:0,module accum(accum,data,ena,clk,rst);output 7:0accum;input 7:0data;input ena,clk,rst;reg 7:0accum;always(posedge clk)begin if(rst)accum=8b0000_0000;else if(ena)accum=data;endendmodule,17.3.4 算术逻辑运算器,助记符 操作码 功能HLT 3b000 暂停SKZ 3b001 累加器为零转移ADD 3b010 加ANDD 3b
11、011 与XORR 3b100 或LDA 3b101 取数STO 3b110 存数JMP 3b111 转移,timescale 1ns/1nsmodule alu(alu_out,zero,data,accum,alu_ena,opcode,clk);output 7:0alu_out;output zero;input 7:0data,accum;input 2:0opcode;input alu_ena,clk;reg 7:0alu_out;parameter HLT=3b000,SKZ=3b001,ADD=3b010,ANDD=3b011,XORR=3b100,LDA=3b101,STO
12、=3b110,JMP=3b111;,assign zero=!accum;always(posedge clk)if(alu_ena)begin casex(opcode)HLT:alu_out=accum;SKZ:alu_out=accum;ADD:alu_out=data+accum;ANDD:alu_out=data endcase end endmodule,17.3.5 数据控制器,data7:0,alu_out7:0,datactl_ena,module datactl(data,in,data_ena);output 7:0data;input 7:0in;input data_
13、ena;assign data=(data_ena)?in:8bz;endmodule,17.3.6 地址多路器,module adr(addr,fetch,pc_addr,ir_addr);input fetch;input 12:0pc_addr,ir_addr;output 12:0addr;assign addr=fetch?pc_addr:ir_addr;endmodule,17.3.7 程序计数器,module counter(pc_addr,ir_addr,load,clk,rst);input load,clk,rst;input 12:0ir_addr;output 12:0
14、pc_addr;reg 12:0pc_addr;always(posedge clk or posedge rst)if(rst)pc_addr=13b0_0000_0000_0000;else if(load)pc_addr=ir_addr;else pc_addr=pc_addr+1;endmodule,17.3.8 状态控制器,timescale 1ns/1nsmodule machinectl(ena,fetch,rst,clk);input fetch,rst,clk;output ena;reg ena;always(posedge clk)begin if(rst)ena=0;e
15、lse if(fetch)ena=1;endendmodule,17.3.8 状态控制器,状态控制器两部分:状态机(machine)状态控制器machinectlrst有效,ena=0,状态机停止工作。,17.3.8 状态控制器,000,001,010,011,100,101,110,111,状态机是CPU的核心部件,用于产生控制信号:启动、停止某些部件CPU执行读指令来读写接口、存储器状态变量state的值,是指令周期已经过的时钟数,指令周期中8个时钟完成的操作,第0个时钟:rd和load_ir为高电平,ROM中高八位指令代码-指令寄存器,第1个时钟:rd和load_ir为高电平,inc_p
16、c加1,故PC+1,ROM中低八位指令代码-指令寄存器,第2个时钟:空操作,第3个时钟:PC+1,指向下一条指令;若HLT指令,halt=1;其它指令控制线输出为0,指令周期中8个时钟完成的操作,第4个时钟:若AND ADD XOR LDA,读相应地址数据;若JMP,目的地址-PC;若STO,输出累加器数据。,第5个时钟:若AND ADD XOR,进行相应运算;若LDA,数据-运算器-累加器;若JMP,锁存目的地址;若STO,数据到地址处。,第6个时钟:空操作,第7个时钟:若SKZ指令,PC+1跳过一条指令;否则PC无变化,timescale 1ns/1nsmodule machine(inc
17、_pc,load_acc,load_pc,rd,wr,load_ir,datactl_ena,halt,clk,zero,ena,opcode);output inc_pc,load_acc,load_pc,rd,wr,load_ir;output datactl_ena,halt;input clk,zero,ena;input 2:0opcode;reg inc_pc,load_acc,load_pc,rd,wr,load_ir;reg datactl_ena,halt;reg 2:0state;parameter HLT=3B000,SKZ=3b001,ADD=3b010,ANDD=3b
18、011,XORR=3b100,LDA=3b101,STO=3b110,JMP=3b111;,always(negedge clk)begin if(!ena)begin state=3b000;inc_pc,load_acc,load_pc,rd=4b0000;wr,load_ir,datactl_ena,halt=4b0000;end else ctl_cycle;end,/-begin of task ctl_cycle-task ctl_cycle;begin casex(state)3b000:begin inc_pc,load_acc,load_pc,rd=4b0001;wr,loa
19、d_ir,datactl_ena,halt=4b0100;state=3b001;end 3b001:begin inc_pc,load_acc,load_pc,rd=4b1001;wr,load_ir,datactl_ena,halt=4b0100;state=3b010;end 3b010:begin inc_pc,load_acc,load_pc,rd=4b0000;wr,load_ir,datactl_ena,halt=4b0000;state=3b011;end,3b011:begin if(opcode=HLT)begin inc_pc,load_acc,load_pc,rd=4b
20、1000;wr,load_ir,datactl_ena,halt=4b0001;state=3b100;end else begin inc_pc,load_acc,load_pc,rd=4b1000;wr,load_ir,datactl_ena,halt=4b0000;state=3b100;end end,3b100:begin if(opcode=JMP)begin inc_pc,load_acc,load_pc,rd=4b0010;wr,load_ir,datactl_ena,halt=4b0000;end else if(opcode=ADD|opcode=ANDD|opcode=X
21、ORR|opcode=LDA)begin inc_pc,load_acc,load_pc,rd=4b0001;wr,load_ir,datactl_ena,halt=4b0000;end else if(opcode=STO)begin inc_pc,load_acc,load_pc,rd=4b0000;wr,load_ir,datactl_ena,halt=4b0010;end else begin inc_pc,load_acc,load_pc,rd=4b0000;wr,load_ir,datactl_ena,halt=4b0000;end state=3b101;end,3b101:be
22、gin/operation if(opcode=ADD|opcode=ANDD|opcode=XORR|opcode=LDA)begin inc_pc,load_acc,load_pc,rd=4b0101;wr,load_ir,datactl_ena,halt=4b0000;end else if(opcode=SKZ end,3b110:/idle begin if(opcode=STO)begin inc_pc,load_acc,load_pc,rd=4b0000;wr,load_ir,datactl_ena,halt=4b0010;end else if(opcode=ADD|opcod
23、e=ANDD|opcode=XORR|opcode=LDA)begin inc_pc,load_acc,load_pc,rd=4b0001;wr,load_ir,datactl_ena,halt=4b0000;end else begin inc_pc,load_acc,load_pc,rd=4b0000;wr,load_ir,datactl_ena,halt=4b0000;end state=3b111;end,3b111:begin if(opcode=SKZ end endcase end endtask/-end of task ctl_cycle-endmodule,cpu,AB,D
24、B,地址译码器,ROM,RAM,clock,reset,MICRO COMPUTER SYSTEM,read,write,17.3.9 外围模块(地址译码器),module addr_decode(addr,rom_sel,ram_sel);output rom_sel,ram_sel;input 12:0addr;reg rom_sel,ram_sel;always(addr)casex(addr)13b1_1xxx_xxxx_xxxx:rom_sel,ram_sel=2b01;13b0_xxxx_xxxx_xxxx:rom_sel,ram_sel=2b10;13b1_0 xxx_xxxx_
25、xxxx:rom_sel,ram_sel=2b10;default:rom_sel,ram_sel=2b00;endcaseendmodule,17.3.9 外围模块(RAM),module addr_decode(addr,rom_sel,ram_sel);output rom_sel,ram_sel;input 12:0addr;reg rom_sel,ram_sel;always(addr)casex(addr)13b1_1xxx_xxxx_xxxx:rom_sel,ram_sel=2b01;13b0_xxxx_xxxx_xxxx:rom_sel,ram_sel=2b10;13b1_0
26、xxx_xxxx_xxxx:rom_sel,ram_sel=2b10;default:rom_sel,ram_sel=2b00;endcaseendmodule,17.3.9 外围模块(ROM),module rom(data,addr,read,ena);output 7:0data;input 12:0addr;input ena,read;reg 7:0mem13h1fff:0;assign data=(readendmodule,17.4 RISC_CPU操作和时序,主要操作:系统的复位和启动操作总线读操作总线写操作,17.4.1 系统的复位和启动操作,复位状态reset=1:CPU内
27、部寄存器全为零数据总线为高阻状态地址总线为0000H所有控制信号为无效状态,启动状态reset=0:fetch上升沿启动CPU开始工作从ROM的000处读指令并执行,17.4.2 总线读操作,复位状态reset=1:CPU内部寄存器全为零数据总线为高阻状态地址总线为0000H所有控制信号为无效状态,启动状态reset=0:fetch上升沿启动CPU开始工作从ROM的000处读指令并执行,D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0,高8位,低8位,地址码,指令码,17.5 RISC_CPU 寻址方式和指令系统,指令数目:8条地址空间
28、:213=8K,17.5 RISC_CPU 寻址方式和指令系统,直接寻址方式数据放在存储器中,寻址单元的地址由指令直接给出。,LDA 010FH,8EH,00H,77H,56H,EDH,FFH,FFH,00H,010FH,010EH,010DH,010CH,56H,1,0,1,0,0,0,0,1,0,0,0,0,1,1,1,1,地址码,指令码,17.5 RISC_CPU 寻址方式和指令系统,HLT:停机操作该操作空一个指令周期,即8个时钟,SKZ:为零跳过下一条语句累加器结果为零,跳过下一语句;累加器结果不为零,顺序执行。,ADD:相加累加器的值+地址所知的存储器中的数据,结果送回累加器。,1
29、7.5 RISC_CPU 寻址方式和指令系统,ANDD:相与累加器的值&地址所知的存储器中的数据,结果送回累加器。,XORR:相异或累加器的值地址所知的存储器中的数据,结果送回累加器。,LDA:读数据地址所知的存储器中的数据送累加器。,17.5 RISC_CPU 寻址方式和指令系统,STO:写数据累加器中的数据送地址所指的存储器中,JMP:无条件跳转语句该操作跳转至指令给出的目的地址,继续执行。,data,load_ir,rd,inc_pc,alu_ena,fetch,clk,0,1,2,3,4,5,6,7,LDA 1000,B0,00,EE,load_acc,address,0000,000
30、1,1000,0002,data,load_ir,rd,inc_pc,alu_ena,fetch,clk,0,1,2,3,4,5,6,7,ADD 1001,50,01,11,load_acc,address,0002,0003,1001,0004,data,load_ir,rd,inc_pc,alu_ena,fetch,clk,0,1,2,3,4,5,6,7,STO 1001,D0,01,FF,wr,address,0004,0005,1001,0006,00111_00000/00 BEGIN:JMP TEST_JMP0011_1100 000_00000/02 HLT0000_000000
31、0_00000/04 HLT0000_0000101_11000/06 JMP_OK:LDA DATA_10000_0000001_00000/08 SKZ0000_0000000_00000/0A HLT0000_0000101_11000/0C LDA DATA_20000_0001001_00000/0E SKZ0000_0000111_00000/10 JMP SKZ_OK0001_0100000_00000/12 HLT0000_0000110_11000/14 SKZ_OK:STO TEMP0000_0010101_11000/16 LDA DTAT_10000_0000110_1
32、1000/18 STO TEMP0000_0010101_11000/1A LDA TEMP0000_0010,001_00000/1C SKZ0000_0000000_00000/1E HLT0000_0000100_11000/20 XORR DTAT_20000_0001001_00000/22 SKZ0000_0000000_00000/24 HLT0000_0000111_00000/26 JMP XORR_OK0010_0100100_11000/28 XORR_OK:XORR DATA_20000_0001001_00000/2A SKZ0000_0000000_00000/2C
33、 HLT0000_0000 000_00000/2E EDN:HLT0000_0000111_00000/30 JMP BEGIN0000_00003C111_00000/3C TST_JMP:JMP JMP_OK0000_0110000_00000/3E HLT,ROM,00 00000000/1800 DATA_1 11111111/1801 DATA_2 10101010/1802 TEMP,RAM,17.6 RISC_CPU 模块的调试,cpu,AB,DB,地址译码器,ROM,RAM,clock,reset,read,write,include clk_gen.vinclude reg
34、ister.vinclude accum.vinclude adr.vinclude alu.vinclude machine.vinclude machinectl.vinclude counter.vinclude datactl.vtimescale 1ns/1nsmodule cpu(clk,reset,halt,rd,wr,addr,data,opcode,fetch,ir_addr,pc_addr);input clk,reset;output rd,wr,halt;output 12:0addr;output 2:0opcode;output fetch;output 12:0i
35、r_addr,pc_addr;inout 7:0data;wire clk,reset,halt;wire 7:0data;wire 12:0addr;wire rd,wr,wire fetch,alu_ena;wire 2:0opcode;wire 12:0ir_addr,pc_addr;wire 7:0alu_out,accum;wire zero,inc_pc,load_acc,load_pc,load_ir,data_ena,contrl_ena;endmodule,module cpu(clk,reset,halt,rd,wr,addr,data,opcode,fetch,ir_ad
36、dr,pc_addr);clk_gen m_clkgen(.clk(clk),.reset(reset),.fetch(fetch),.alu_ena(alu_ena);register m_register(.data(data),.ena(load_ir),.rst(reset),.clk(clk),.opc_iraddr(opcode,ir_addr);accum m_accum(.data(alu_out),.ena(load_acc),.clk(clk),.rst(reset),.accum(accum);alu m_alu(.data(data),.accum(accum),.cl
37、k(clk),.alu_ena(alu_ena),.opcode(opcode),.alu_out(alu_out),.zero(zero);machinectl m_machinectl(.clk(clk),.rst(reset),.fetch(fetch),.ena(contrl_ena);machine m_machine(.inc_pc(inc_pc),.load_acc(load_acc),.load_pc(load_pc),.rd(rd),.wr(wr),.load_ir(load_ir),.clk(clk),.datactl_ena(data_ena),.halt(halt),.
38、zero(zero),.ena(contrl_ena),.opcode(opcode);datactl m_datactl(.in(alu_out),.data_ena(data_ena),.data(data);adr m_adr(.fetch(fetch),.ir_addr(ir_addr),.pc_addr(pc_addr),.addr(addr);counter m_counter(.clk(inc_pc),.rst(reset),.ir_addr(ir_addr),.load(load_pc),.pc_addr(pc_addr);endmodule,include cpu.vincl
39、ude ram.vinclude rom.vinclude addr_decode.vtimescale 1ns/1nsdefine PERIOD 100module topcpu;reg reset_req,clock;integer test;reg(3*8):0mnemonic;reg 12:0PC_addr,IR_addr;wire 7:0data;wire 12:0addr;wire rd,wr,halt,ram_sel,rom_sel;wire 2:0opcode;wire fetch;wire 12:0ir_addr,pc_addr;.endmodule,module topcp
40、u;reg reset_req,clock;.cpu t_cpu(.clk(clock),.reset(reset_req),.halt(halt),.rd(rd),.wr(wr),.addr(addr),.data(data),.opcode(opcode),.fetch(fetch),.ir_addr(ir_addr),.pc_addr(pc_addr);ram t_ram(.addr(addr9:0),.read(rd),.write(wr),.ena(ram_sel),.data(data);rom t_rom(.addr(addr),.read(rd),.ena(rom_sel),.
41、data(data);addr_decode t_addr_decode(.addr(addr),.ram_sel(ram_sel),.rom_sel(rom_sel);initial begin clock=1;$timeformat(-9,1,ns,12);display_debug_message;sys_reset;test1;$stop;$finish;end.endmodule,module topcpu;.task display_debug_message;begin$display(n*);$display(*test1;to load the 1st diagnostic
42、program.*);end endtask task test1;begin test=0;disable MONITOR;$readmemb(test1.pro,t_rom.mem);$display(rom loaded successfully!);$readmemb(test1.dat,t_ram.mem);$display(ram loaded successfully!);#1 test=1;#14800;sys_reset;end endtask.endmodule,module topcpu;task sys_reset;begin reset_req=0;#(PERIOD*
43、0.7)reset_req=1;#(1.5*PERIOD)reset_req=0;end endtask always(test)begin:MONITOR case(test)1:begin$display(n*RUNNING CPUtest1-The Basic CPU Diagonostic Program*);$display(n TIME PC INSTR ADDR DATA);$display(-);while(test=1)(t_cpu.pc_addr)if(t_cpu.pc_addr%2=1)end end endcase endendmodule,module topcpu;
44、.always(posedge halt)begin#500$display(n*);$display(*A HALT INSTRUCTION WAS PROCESSD!*);$display(*);end always#(PERIOD/2)clock=clock;always(t_cpu.opcode)case(t_cpu.opcode)3b000:mnemonic=HLT;3b001:mnemonic=SKZ;3b010:mnemonic=ADD;3b011:mnemonic=AND;3b100:mnemonic=XOR;3b101:mnemonic=LDA;3b110:mnemonic=STO;3b111:mnemonic=JMP;endcase endmodule,