题目如下:
自己写的代码比较繁琐:
module top_module (
input too_cold,
input too_hot,
input mode,
input fan_on,
output heater,
output aircon,
output fan
);
always @(*)begin
heater = 1'b0;
aircon = 1'b0;
fan = 1'b0;
if(mode==1'b1)begin
if(too_cold==1)begin
aircon = 1'b0;
heater = 1'b1;
fan = 1'b1;
end
end
if(mode==1'b0)begin
if(too_hot==1)begin
aircon = 1'b1;
heater = 1'b0;
fan = 1'b1;
end
end
if(fan_on) fan=1'b1;
end
endmodule
而标准答案比较简单:
module top_module (
input too_cold,
input too_hot,
input mode,
input fan_on,
output heater,
output aircon,
output fan
);
assign heater = mode & too_cold;
assign aircon = (~mode) & too_hot;
assign fan = (heater | aircon) | fan_on;
endmodule