机器状态不改变输出
Machine state does not change output
正如您在下面的代码中看到的,我有一个机器状态,其状态称为 start。此状态应采用 coord_x_reg 的值,将其递增 1 并使用 assign 指令将其分配给输出端口结尾。问题是:
输出 coord_x 在模拟中没有变化,正如您在照片中看到的那样
我想问题是我不能同时写入和读取寄存器。那我怎样才能达到我想要的效果呢? (我也试过使用 coord_x_reg 作为整数变量)
使用重置时,我必须注释一些信号,否则我会得到 非阻塞赋值的非法左侧 错误。
module Ball
#(parameter SCR_W = 1280, //SCREEN RESOLUTION?
parameter SCR_H = 720,
parameter BALL_WIDTH = 30,
parameter PAD_Y_WIDTH = 26,
parameter PAD_Y_LENGTH = 168,
parameter PAD_x_POS_L = 67,
parameter PAD_x_POS_R = 1213 //SCR_W-PAD_x_POS_L
)
(
input wire CLK, RST,
input wire button_L, button_R,
input wire [9:0] pad_y_L,
input wire [9:0] pad_y_R,
output wire [9:0] coord_x,
output wire [9:0] coord_y,
output wire [3:0] score_left,
output wire [3:0] score_right
);
//STATES
localparam [3:0] //11 states needed we need 4 bits
start = 4'b0000,
start_race = 4'b0001,
left_player_begins = 4'b0010,
right_player_begins = 4'b0011,
top_left = 4'b0100,
top_right = 4'b0101,
bottom_left = 4'b0110,
bottom_right = 4'b0111,
score_L = 4'b1000,
score_R = 4'b1001,
game_over = 4'b1010;
//Current state variables
reg [3:0] state_reg, state_next;
//output registers
reg [9:0] coord_x_reg = 0;
reg [9:0] coord_y_reg = 0;
reg [3:0] score_left_reg = 0;
reg [3:0] score_right_reg = 0;
always @(posedge CLK, posedge RST)
begin
if(RST) // go to state zero if reset
begin
state_reg <= start;
//coord_x <= 0;
//coord_y <= 0;
//score_left <= 0;
//score_right <= 0;
end
else // otherwise update the states
begin
state_reg <= state_next;
end
end
//_________________________________________________________________________
// MACHINE STATE DESIGN
//_________________________________________________________________________
always @(state_reg) // <------------ PUT VARIABLES HERE???????!!!!!!!!!!!!!!!
begin
// store current state as next
state_next = state_reg; // REQUIRED: when no case statement is satisfied
case(state_reg)
start :
begin
coord_x_reg <= coord_x_reg + 1;
if (coord_x_reg == 10)
state_next = start_race;
end
start_race :
begin
state_next = start;
end
endcase
end
assign coord_x = coord_x_reg;
assign coord_y = coord_y_reg;
assign score_left = score_left_reg;
assign score_right = score_right_reg;
endmodule // Ball
通过调查,我得出的结论是敏感性列表出了问题。我修改了代码,使机器状态在 always @(posedge CLK, posedge RST)
内。现在按预期工作。新代码:
module Ball
#(parameter SCR_W = 1280, //SCREEN RESOLUTION?
parameter SCR_H = 720,
parameter BALL_WIDTH = 30,
parameter PAD_Y_WIDTH = 26,
parameter PAD_Y_LENGTH = 168,
parameter PAD_x_POS_L = 67,
parameter PAD_x_POS_R = 1213 //SCR_W-PAD_x_POS_L
)
(
input wire CLK, RST,
input wire button_L, button_R,
input wire [9:0] pad_y_L,
input wire [9:0] pad_y_R,
output wire [9:0] coord_x,
output wire [9:0] coord_y,
output wire [3:0] score_left,
output wire [3:0] score_right
);
//STATES
localparam [3:0] //11 states needed we need 4 bits
start = 4'b0000,
start_race = 4'b0001,
left_player_begins = 4'b0010,
right_player_begins = 4'b0011,
top_left = 4'b0100,
top_right = 4'b0101,
bottom_left = 4'b0110,
bottom_right = 4'b0111,
score_L = 4'b1000,
score_R = 4'b1001,
game_over = 4'b1010;
//Current state variables
reg [3:0] state;
//output registers
reg [9:0] coord_x_reg = 0;
reg [9:0] coord_y_reg = 0;
reg [3:0] score_left_reg = 0;
reg [3:0] score_right_reg = 0;
always @(posedge CLK, posedge RST)
begin
if(RST) // go to state zero if reset
begin
state <= start;
coord_x <= 0;
coord_y <= 0;
score_left <= 0;
score_right <= 0;
end
else // otherwise update the states
begin
case(state)
start :
begin
coord_x_reg = coord_x_reg + 1;
if (coord_x_reg == 10)
state = start_race;
end
start_race :
begin
state = start_race;
end
endcase
end
end
assign coord_x = coord_x_reg;
assign coord_y = coord_y_reg;
assign score_left = score_left_reg;
assign score_right = score_right_reg;
endmodule // Ball ```
正如您在下面的代码中看到的,我有一个机器状态,其状态称为 start。此状态应采用 coord_x_reg 的值,将其递增 1 并使用 assign 指令将其分配给输出端口结尾。问题是:
输出 coord_x 在模拟中没有变化,正如您在照片中看到的那样
我想问题是我不能同时写入和读取寄存器。那我怎样才能达到我想要的效果呢? (我也试过使用 coord_x_reg 作为整数变量)使用重置时,我必须注释一些信号,否则我会得到 非阻塞赋值的非法左侧 错误。
module Ball
#(parameter SCR_W = 1280, //SCREEN RESOLUTION?
parameter SCR_H = 720,
parameter BALL_WIDTH = 30,
parameter PAD_Y_WIDTH = 26,
parameter PAD_Y_LENGTH = 168,
parameter PAD_x_POS_L = 67,
parameter PAD_x_POS_R = 1213 //SCR_W-PAD_x_POS_L
)
(
input wire CLK, RST,
input wire button_L, button_R,
input wire [9:0] pad_y_L,
input wire [9:0] pad_y_R,
output wire [9:0] coord_x,
output wire [9:0] coord_y,
output wire [3:0] score_left,
output wire [3:0] score_right
);
//STATES
localparam [3:0] //11 states needed we need 4 bits
start = 4'b0000,
start_race = 4'b0001,
left_player_begins = 4'b0010,
right_player_begins = 4'b0011,
top_left = 4'b0100,
top_right = 4'b0101,
bottom_left = 4'b0110,
bottom_right = 4'b0111,
score_L = 4'b1000,
score_R = 4'b1001,
game_over = 4'b1010;
//Current state variables
reg [3:0] state_reg, state_next;
//output registers
reg [9:0] coord_x_reg = 0;
reg [9:0] coord_y_reg = 0;
reg [3:0] score_left_reg = 0;
reg [3:0] score_right_reg = 0;
always @(posedge CLK, posedge RST)
begin
if(RST) // go to state zero if reset
begin
state_reg <= start;
//coord_x <= 0;
//coord_y <= 0;
//score_left <= 0;
//score_right <= 0;
end
else // otherwise update the states
begin
state_reg <= state_next;
end
end
//_________________________________________________________________________
// MACHINE STATE DESIGN
//_________________________________________________________________________
always @(state_reg) // <------------ PUT VARIABLES HERE???????!!!!!!!!!!!!!!!
begin
// store current state as next
state_next = state_reg; // REQUIRED: when no case statement is satisfied
case(state_reg)
start :
begin
coord_x_reg <= coord_x_reg + 1;
if (coord_x_reg == 10)
state_next = start_race;
end
start_race :
begin
state_next = start;
end
endcase
end
assign coord_x = coord_x_reg;
assign coord_y = coord_y_reg;
assign score_left = score_left_reg;
assign score_right = score_right_reg;
endmodule // Ball
通过调查,我得出的结论是敏感性列表出了问题。我修改了代码,使机器状态在 always @(posedge CLK, posedge RST)
内。现在按预期工作。新代码:
module Ball
#(parameter SCR_W = 1280, //SCREEN RESOLUTION?
parameter SCR_H = 720,
parameter BALL_WIDTH = 30,
parameter PAD_Y_WIDTH = 26,
parameter PAD_Y_LENGTH = 168,
parameter PAD_x_POS_L = 67,
parameter PAD_x_POS_R = 1213 //SCR_W-PAD_x_POS_L
)
(
input wire CLK, RST,
input wire button_L, button_R,
input wire [9:0] pad_y_L,
input wire [9:0] pad_y_R,
output wire [9:0] coord_x,
output wire [9:0] coord_y,
output wire [3:0] score_left,
output wire [3:0] score_right
);
//STATES
localparam [3:0] //11 states needed we need 4 bits
start = 4'b0000,
start_race = 4'b0001,
left_player_begins = 4'b0010,
right_player_begins = 4'b0011,
top_left = 4'b0100,
top_right = 4'b0101,
bottom_left = 4'b0110,
bottom_right = 4'b0111,
score_L = 4'b1000,
score_R = 4'b1001,
game_over = 4'b1010;
//Current state variables
reg [3:0] state;
//output registers
reg [9:0] coord_x_reg = 0;
reg [9:0] coord_y_reg = 0;
reg [3:0] score_left_reg = 0;
reg [3:0] score_right_reg = 0;
always @(posedge CLK, posedge RST)
begin
if(RST) // go to state zero if reset
begin
state <= start;
coord_x <= 0;
coord_y <= 0;
score_left <= 0;
score_right <= 0;
end
else // otherwise update the states
begin
case(state)
start :
begin
coord_x_reg = coord_x_reg + 1;
if (coord_x_reg == 10)
state = start_race;
end
start_race :
begin
state = start_race;
end
endcase
end
end
assign coord_x = coord_x_reg;
assign coord_y = coord_y_reg;
assign score_left = score_left_reg;
assign score_right = score_right_reg;
endmodule // Ball ```