summaryrefslogtreecommitdiff
path: root/nandgame/alu.sv
blob: 54297c9c97963a1854909ef4a0c06906b1a6c085 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// nandgame ALU

`timescale 1us/1us

`include "nandgame_types.v"
`include "arith_unit.sv"
`include "logic_unit.sv"

`ifndef NANDGAME_ALU
`define NANDGAME_ALU

module  alu #(
  parameter DATA_WIDTH = 16
) (
  // "X" operand
  input [(DATA_WIDTH-1):0] X_in,
  // "Y" operand
  input [(DATA_WIDTH-1):0] Y_in,
  // "u" flag. 1=arithmetic, 0=logic operation
  input logic u_arith_nlogic_in,
  // opcode, see ArithCode / LogicCode
  input logic [1:0] opcode_in,
  // zero the "X" operand
  input logic zx_in,
  // swap "X" and "Y" operands
  input logic sw_in,

  // result of operation
  output logic [(DATA_WIDTH-1):0] result_out
);

logic [(DATA_WIDTH-1):0] int_op_x /* verilator public */;
logic [(DATA_WIDTH-1):0] int_op_y /* verilator public */;

logic [(DATA_WIDTH-1):0] int_result_arith;
logic [(DATA_WIDTH-1):0] int_result_logic;

assign int_op_x = zx_in ? 0
                  : (sw_in ? Y_in : X_in);
assign int_op_y = sw_in ? X_in : Y_in;

arith_unit au (
  .X_in(int_op_x),
  .Y_in(int_op_y),
  .result_out(int_result_arith),
  .arith_operation_in(ArithCode'(opcode_in))
);

logic_unit lu (
  .X_in(int_op_x),
  .Y_in(int_op_y),
  .result_out(int_result_logic),
  .logic_operation_in(LogicCode'(opcode_in))
);

assign result_out = u_arith_nlogic_in ? int_result_arith : int_result_logic;

endmodule

`endif