always@(cu_state, pc, md_rd)begin case(cu_state) 2'b00: begin //reset cu_state_tmp = 2'b01; pc_tmp = pc; ir_tmp = 16'b0; acc_tmp = acc; mar = 13'b0; wr = 1'b0; rd = 1'b0; md_wr = 8'b0; end 2'b01: begin cu_state_tmp = 2'b10; //cu_state_tmp = cu_state_tmp + 1 pc_tmp = pc + 13'b1; ir_tmp = {8'b0, md_rd}; acc_tmp = acc; mar = pc; wr = 1'b0; rd = 1'b1; md_wr = 8'b0; end 2'b10: begin cu_state_tmp = 2'b11; //cu_state_tmp = cu_state_tmp + 1 pc_tmp = pc + 13'b1; ir_tmp = {md_rd, ir[7:0]}; acc_tmp = acc; mar = pc; wr = 1'b0; rd = 1'b1; md_wr = 8'b0; end 2'b11: begin cu_state_tmp = 2'b01; ir_tmp = 16'b0; pc_tmp = pc; //mar = 13'b0; //wr = 1'b0; //rd = 1'b0; //md_wr = 8'b0; ALU; end default: begin cu_state_tmp = 2'b00; pc_tmp = 8'b0; ir_tmp = 16'b0; acc_tmp = acc; mar = 13'b0; wr = 1'b0; rd = 1'b0; md_wr = 8'b0; end endcase end加入间接寻址周期
时间: 2024-03-11 09:49:17 浏览: 60
在这段代码中,加入间接寻址周期可以通过在cu_state为特定状态时,设置mar变量的值为acc寄存器中存储的内容。具体实现方法如下:
在case语句中添加一个新的分支,当cu_state为特定状态时执行间接寻址周期:
```
2'b11: begin // 间接寻址周期
cu_state_tmp = 2'b01;
ir_tmp = 16'b0;
pc_tmp = pc;
mar = acc; // 设置mar为acc寄存器中存储的内容
wr = 1'b0;
rd = 1'b1;
md_wr = 8'b0;
end
```
需要注意的是,间接寻址周期在cu_state为2'b11时执行,因此需要在此分支中执行ALU操作,而不是在默认分支中执行。
相关问题
改写一下这段代码,使得寄存器地址支持16bit读写,现在这段是只支持8bit读写://FSM always @ (posedge clk or negedge rst) if (~rst) i2c_state<=3'b000;//idle else i2c_state<= next_i2c_state; //////////Modified on 25 november.write Address is 30H; Read Address is 31H///// always @(i2c_state or stopf or startf or cnt or sft or sadr or hf or scl_neg or cnt) case(i2c_state) 3'b000: //This state is the initial state,idle state begin if (startf)next_i2c_state<= 3 b001;//start else next_i2c_state <= i2c_state; end 3b001://This state is the device address detect & trigger begin if(stopf)next_i2c_state<=3'b000; else begin if((cnt==4'h9)&&({sft[0],hf} ==2'b00) && (scl_neg ==1'b1)&&(sadr ==sft[7:1])) next i2c_ state<=3'b010;//write: i2c adderss is 00110000 and ACK is sampled //so {sft[0],hf} is 2'b00 else if ((cnt==4'h9)&&({sft[0],hf} ==2'b10) && (scl_neg ==1'b1)&&(sadr ==sft[7:1])) next i2c_ state<=3'b011;//read:i2c adderss is 00110001 and ACK is sampled //so {sft[0],hf} is 2'b10 else if((cnt ==4'h9) && (scl_neg == 1'b1)) next_ i2c_state<=3 'b000;//when the address accepted does not match the SADR, //the state comes back else next_i2c_state<=i2c_state; end end 3'b010: //This state is the register address detect &&trigger begin if (stopf)next_i2c_state<=3'b000; else if (startf)next_i2c_state<=3'b001; else if ((cnt ==4'h9) && (scl_neg == 1'b1)) next_i2c _state<=3'b10 else next i2c_state<=i2c_state; end 3'b011: //This state is the register data read begin if (stopf)next_i2c _state<=3'b000; else if (startf) next_i2c _state<=3'b001; else next_12c_state<=i2c_state; end 3'b100: //This state is the register data write begin if (stopf)next_i2c _state<=3'b000; else if (startf) next_i2c _state<=3b001; else next_i2c_state<=i2c_state; end default://safe mode control next_i2c_state <= 3'b000; endcase
以下是修改后的代码,支持16bit读写:
//FSM always @ (posedge clk or negedge rst) if (~rst) i2c_state<=3'b000;//idle else i2c_state<= next_i2c_state; //////////Modified on 25 november.write Address is 30H; Read Address is 31H///// always @(i2c_state or stopf or startf or cnt or sft or sadr or hf or scl_neg or cnt) case(i2c_state) 3'b000: //This state is the initial state,idle state begin if (startf)next_i2c_state<= 3 b001;//start else next_i2c_state <= i2c_state; end 3b001://This state is the device address detect & trigger begin if(stopf)next_i2c_state<=3'b000; else begin if((cnt==4'h9)&&({sft[0],hf} ==2'b00) && (scl_neg ==1'b1)&&(sadr ==sft[15:2])) next i2c_ state<=3'b010;//write: i2c adderss is 00110000 and ACK is sampled //so {sft[0],hf} is 2'b00 else if ((cnt==4'h9)&&({sft[0],hf} ==2'b10) && (scl_neg ==1'b1)&&(sadr ==sft[15:2])) next i2c_ state<=3'b011;//read:i2c adderss is 00110001 and ACK is sampled //so {sft[0],hf} is 2'b10 else if((cnt ==4'h9) && (scl_neg == 1'b1)) next_ i2c_state<=3 'b000;//when the address accepted does not match the SADR, //the state comes back else next_i2c_state<=i2c_state; end end 3'b010: //This state is the register address detect &&trigger begin if (stopf)next_i2c_state<=3'b000; else if (startf)next_i2c_state<=3'b001; else if ((cnt ==4'h9) && (scl_neg == 1'b1)) next_i2c _state<=3'b10 else next i2c_state<=i2c_state; end 3'b011: //This state is the register data read begin if (stopf)next_i2c _state<=3'b000; else if (startf) next_i2c _state<=3'b001; else next_12c_state<=i2c_state; end 3'b100: //This state is the register data write begin if (stopf)next_i2c _state<=3'b000; else if (startf) next_i2c _state<=3b001; else next_i2c_state<=i2c_state; end default://safe mode control next_i2c_state <= 3'b000; endcase
修改的内容:
1. 在设备地址检测&触发状态中,将设备地址的位数扩展到16位,即将sft[7:1]修改为sft[15:2]。
2. 在写寄存器地址检测&触发状态中,同样将地址的位数扩展到16位。
3. 在读寄存器数据状态和写寄存器数据状态中,没有需要修改的地方。
module data_packetizer ( input clk, // 时钟信号 input rst, // 复位信号 input [15:0] data_in, // 待传输数据 output reg tx_wire, //传输数据线 output reg ready // 数据包就绪信号 ); reg [1:0] state = 2'b00; // 状态机状态变量 reg [35:0] packet_out; // 输出数据包 reg parity; //校验 always @(posedge clk) begin if (rst) begin state <= 2'b00; ready <= 0; packet_out <= 36'h000000; end else begin case(state) 2'b00: begin // 等待输入 ready <= 0; if (data_in != 0) begin state <= 2'b01; end end 2'b01: begin // 第一次加密 packet_out[32:17] <= data_in; state <= 2'b10; end 2'b10: begin // 等待第二个数据 if (data_in != 0) begin state <= 2'b11; end end 2'b11: begin // 第二次加密 packet_out[16:1] <= data_in; packet_out[35:33] <= 3'b101; // 数据包头 packet_out[0] <= ^packet_out[34:3]; // 奇偶校验位 ready <= 1; state <= 2'b00; end endcase end end reg tx_state; //定义发送状态 reg tx_stop; //一个包发送完结束标志 always @(posedge clk) begin if (rst) tx_state<=0; else if(tx_stop) tx_state<=0; else if(ready) tx_state<=1; else tx_state<=tx_state; end reg [6:0]cnt; always @(posedge clk) begin if (rst)begin tx_wire<=1'b1; tx_stop<=1'b0; cnt<='d35; end else if(tx_state)begin if(cnt==0)begin tx_wire<=packet_out[cnt]; tx_stop<=1'b1; cnt<='d35; end else begin cnt<=cnt-1'b1; tx_stop<=1'b0; tx_wire<=packet_out[cnt]; end end else begin tx_wire<=1'b1; tx_stop<=1'b0; cnt<='d35; end end endmodule
这是一个 Verilog HDL 实现的数据包装器模块,用于将输入的数据打包成一个数据包并进行传输。该模块包括一个状态机和一个数据发送器,其中状态机负责控制打包过程中的状态转换,数据发送器则负责将打包好的数据通过传输线发送出去。
具体来说,模块的输入包括时钟信号 clk、复位信号 rst 和待传输的数据 data_in;输出包括传输数据线 tx_wire 和数据包就绪信号 ready。模块内部还定义了一些中间变量,如状态机状态变量 state、数据包 packet_out 以及奇偶校验位 parity 等。
在模块的 always 块中,首先对复位信号进行处理,将状态机状态变量 state 和数据包 packet_out 置为初始值。然后根据状态机的不同状态,对输入的数据进行处理,包括等待输入、第一次加密、等待第二个数据和第二次加密等。最后生成数据包头、奇偶校验位和数据包就绪信号。
在数据发送器中,根据当前的发送状态和计数器值,将打包好的数据通过传输线发送出去。如果当前发送状态为 1,表示有数据包需要发送,则将计数器递减,同时将传输数据线上的数据更新为数据包中的对应数据。如果计数器为 0,则表示一个数据包已经发送完毕,需要将传输数据线上的数据置为 1,同时将结束标志 tx_stop 置为 1。
总的来说,该模块实现了将输入的数据打包成一个数据包并进行传输的功能,可以用于各种需要进行数据传输的场合。
阅读全文