多周期CPU设计——流水灯

本文详细介绍了如何设计一个多周期CPU控制流水灯的实现过程,包括10个关键模块的代码实现。流水灯的变换以1s的时间间隔进行,从0000000到11111111逐步递增,展示了完整的二进制计数变化过程。
摘要由CSDN通过智能技术生成

本设计分为10个模块加一个整体仿真激励模块,各模块代码如下:

流水灯的变换形式为: 0000000 -> 00000001 -> 00000011 -> 00000111 -> 00001111 -> 00011111 -> 00111111 -> 01111111 -> 11111111

时间间隔为1s。


`timescale 1ns / 1ps

//包括IR

module InsMem(
	input CLK,
	input InsMemRW, IRWre,
	input [31:0] pc_out,
	output reg [31:0] ins_out
    );

	reg [31:0] IDataOut;
	reg [7:0] InsMemory[0:255];
	
	initial begin
		$readmemb("instructions.txt", InsMemory);
	end
	
	always @ (pc_out or InsMemRW) begin
		if (InsMemRW) begin
			IDataOut[31:24] =  InsMemory[pc_out];
			IDataOut[23:16] =  InsMemory[pc_out+1];
			IDataOut[15:8]  =  InsMemory[pc_out+2];
			IDataOut[7:0]   =  InsMemory[pc_out+3];
		end
	end
	
	always @ (posedge CLK) begin
		if (IRWre)
			ins_out <= IDataOut;
	end
	
endmodule

module fp50m(clk,reset,newclk);
	input clk,reset;
	output newclk;
	reg newclk;
	reg [31:0] count,count1;
	always@(posedge clk or negedge reset)
	begin
		if(!reset)
		begin 
			newclk<=1'd0; 
			count<=32'd0;
		end 
		else if(count==32'd1250000)  //0.1s 2500000
		begin 
			count<=32'd0; 
			newclk<=~newclk;
		end//end begin 
		else count<=count+1'd1;
	end
endmodule

`timescale 1ns / 1ps

module Extend(
	input ExtSel,
	input [15:0] Imm_16,
	output reg [31:0] Imm
    );
	
	always @ (Imm_16 or ExtSel) begin
		if (ExtSel) begin
			Imm = {
  {16{Imm_16[15]}}, Imm_16[15:0]};
		end else begin
			Imm = {
  {16{0}}, Imm_16[15:0]};
		end
	end

endmodule

`timescale 1ns / 1ps

//包括2选1模块,一个缓冲

module DataMem(
	input RD, WR, CLK,
	input ALUM2Reg,
	input [31:0] alu_out,
	input [31:0] result,
	input [31:0] reg_out2,
	output wire [31:0] data_out
    );
	
	reg [31:0] DataOut;
	reg [7:0] DataMemory[0:63];

	assign data_out = (ALUM2Reg ? DataOut : result);
	always @ (alu_out or reg_out2 or RD or WR) begin
		if (WR) begin //写入
			DataMemory[alu_out]   = reg_out2[31:24];
			DataMemory[alu_out+1] = reg_out2[23:16];
			DataMemory[alu_out+2] = reg_out2[15:8];
			DataMemory[alu_out+3] = reg_out2[7:0];
		end else begin //读出
			DataOut[31:24] = DataMemory[alu_out];
			DataOut[23:16] = DataMemory[alu_out+1];
			DataOut[15:8]  = DataMemory[alu_out+2];
			DataOut[7:0]   = DataMemory[alu_out+3];
		end
		//if (ALUM2Reg) data_out = DataOut;
		//else data_out = result;
	end
	
endmodule

`timescale 1ns / 1ps

module ControlUnit(
	input CLK, RST,
	input zero,
	input [5:0] op,
	output reg WrRegData, RegWre, ALUSrcA, ALUSrcB, InsMemRW, IRWre, PCWre, RD, WR, ExtSel, ALUM2Reg, 
	output reg [1:0] RegDst, PCSrc,
	output reg [2:0] ALUOp, state
    );
	
	reg [2:0] next_state;
	
	parameter [2:0] IF = 3'b000,    
						 ID = 3'b001,
						 EXE_1 = 3'b110,
						 EXE_2 = 3'b101,
						 EXE_3 = 3'b010,
						 ME
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
对于在STM32F407上实现流水灯,你可以按照以下步骤进行操作: 1. 首先,需要配置GPIO引脚为输出模式,并设置相应的引脚电平。可以使用STM32CubeMX工具来生成初始化代码,或者手动编写代码进行配置。 2. 创建一个循环,在循环中依次将各个引脚置高电平,然后延时一段时间,再依次将引脚置低电平,再延时一段时间。 3. 重复上述步骤,就可以实现流水灯效果。 下面是一个简单的示例代码,演示了如何在STM32F407上实现流水灯效果: ```c #include "stm32f4xx.h" #include "stm32f4xx_hal.h" // 定义LED引脚 #define LED_PIN_1 GPIO_PIN_12 #define LED_PIN_2 GPIO_PIN_13 #define LED_PIN_3 GPIO_PIN_14 #define LED_PIN_4 GPIO_PIN_15 // 定义延时时间 #define DELAY_MS 500 int main(void) { // 初始化HAL库 HAL_Init(); // 初始化GPIO库 __HAL_RCC_GPIOA_CLK_ENABLE(); GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.Pin = LED_PIN_1 | LED_PIN_2 | LED_PIN_3 | LED_PIN_4; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); // 循环流水灯 while (1) { // 依次点亮LED HAL_GPIO_WritePin(GPIOA, LED_PIN_1, GPIO_PIN_SET); HAL_Delay(DELAY_MS); HAL_GPIO_WritePin(GPIOA, LED_PIN_2, GPIO_PIN_SET); HAL_Delay(DELAY_MS); HAL_GPIO_WritePin(GPIOA, LED_PIN_3, GPIO_PIN_SET); HAL_Delay(DELAY_MS); HAL_GPIO_WritePin(GPIOA, LED_PIN_4, GPIO_PIN_SET); HAL_Delay(DELAY_MS); // 依次熄灭LED HAL_GPIO_WritePin(GPIOA, LED_PIN_1 | LED_PIN_2 | LED_PIN_3 | LED_PIN_4, GPIO_PIN_RESET); HAL_Delay(DELAY_MS); } } ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值