I tried this
module sum(X,Y,Q);
input [31:0] X,Y;
output [31:0] Q;
assign Q=X+Y;
endmodule
module register(loadD, clk, rst, Q);
input [31:0] loadD;
input clk, rst;
output reg [31:0] Q;
always@(posedge clk, posedge rst) begin
if(rst) begin
Q<=0;
end
else if(clk) begin
Q<=loadD;
end
else begin
Q<=Q<<1;
end
end
endmodule
module multiplier(A,B,andS,regS,clk,rst);
input [31:0] A,B;
input clk,rst;
output [31:0] andS, regS;
sum BL1(A,B,andS);
register BL2(andS,clk,rst,regS);
assign A=regS&{32{B[0]}};
endmodule
But don't work
-
\$\begingroup\$ What situation do you expect to lead to the shift operation happening in the register module? Re-read your code and see if it will do what you expect. \$\endgroup\$The Photon– The Photon2019年09月01日 14:32:06 +00:00Commented Sep 1, 2019 at 14:32
-
\$\begingroup\$ Feels like homework to me. Do you know the shift, add, multiply algorithm? Because what is here is a far from what you need. \$\endgroup\$Oldfart– Oldfart2019年09月01日 14:32:09 +00:00Commented Sep 1, 2019 at 14:32
-
\$\begingroup\$ hm, I'll try fix the code. The simulation is not what I expect.. the code is just doing a sum. \$\endgroup\$Alan S– Alan S2019年09月01日 14:39:21 +00:00Commented Sep 1, 2019 at 14:39
3 Answers 3
If there is a
posedge clk
in the sensitivity list of an always block, the condition
if(clk)
will be always true. And
else begin
Q<=Q<<1;
end
will never be deployed.
The logic in your register
module seems incorrect. The sensitivity list you use requires a rising edge on either clk
or rst
, so one or both of these will be high when the module is activated. Now look at your if...else if...else
logic and think about how you want that to work.
As pointed out by Elliot, The problem appears in the lines
if(rst) begin
Q<=0;
end
else if(clk) begin
Q<=loadD;
end
else begin
Q<=Q<<1;
end
If you are detecting both the clock and reset in the sensitivity list, then why you want to check again for a clock? Read a similar answer here: Clock usage in the always block and in the event