blob: 68f5c236824d8e166692065081c93922b425b700 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
// nandgame logic unit
`timescale 1us/1us
`include "nandgame_types.v"
`ifndef NANDGAME_NANDGAME_LOU
`define NANDGAME_NANDGAME_LOU
module logic_unit #(
parameter DATA_WIDTH = 16
) (
// first operand
input [(DATA_WIDTH-1):0] X_in,
// second operand
input [(DATA_WIDTH-1):0] Y_in,
// opcode, see LogicCode
input LogicCode logic_operation_in,
// result of operation
output logic [(DATA_WIDTH-1):0] result_out
);
// learning: instead of this nested ternary...
// assign result_out = logic_operation_in == LOGIC_AND ? (X_in & Y_in) :
// logic_operation_in == LOGIC_OR ? (X_in | Y_in) :
// logic_operation_in == LOGIC_XOR ? (X_in ^ Y_in) :
// logic_operation_in == LOGIC_NEGT ? (~X_in) : 0;
// ... you can do this:
always_comb begin
case (logic_operation_in)
LOGIC_AND: result_out = X_in & Y_in;
LOGIC_OR: result_out = X_in | Y_in;
LOGIC_XOR: result_out = X_in ^ Y_in;
LOGIC_NEGT: result_out = ~X_in;
default: result_out = 0;
endcase
end
endmodule
`endif
|