Compare commits
3 Commits
69c367c344
...
ef62808821
Author | SHA1 | Date | |
---|---|---|---|
ef62808821 | |||
1f7f07b366 | |||
fb65ccc713 |
|
@ -15,9 +15,9 @@
|
||||||
vflags = ''-Wpedantic -Wwarn-lint -Wwarn-style -Wno-PINCONNECTEMPTY -CFLAGS "-Wpedantic -std=c++20"'';
|
vflags = ''-Wpedantic -Wwarn-lint -Wwarn-style -Wno-PINCONNECTEMPTY -CFLAGS "-Wpedantic -std=c++20"'';
|
||||||
|
|
||||||
verilate-src = cmd: ''
|
verilate-src = cmd: ''
|
||||||
cp -r ${./verilog-src} ./verilog-src
|
cp -r ${./src} ./src
|
||||||
cp -r ${./simulation} ./simulation
|
cp -r ${./simulation} ./simulation
|
||||||
find ./verilog-src/ -name '*.v' -exec ${verilator}/bin/verilator ${vflags} ${cmd} {} +
|
find ./src/ -name '*.v' -exec ${verilator}/bin/verilator ${vflags} ${cmd} {} +
|
||||||
'';
|
'';
|
||||||
|
|
||||||
lint = pkgs.runCommand "lint" {} ''
|
lint = pkgs.runCommand "lint" {} ''
|
||||||
|
@ -27,14 +27,14 @@
|
||||||
'';
|
'';
|
||||||
|
|
||||||
alu-sim = pkgs.runCommandCC "alu-sim" {} ''
|
alu-sim = pkgs.runCommandCC "alu-sim" {} ''
|
||||||
${verilate-src "--cc --build --exe ./simulation/tester.cpp ./simulation/test_alu.cpp"}
|
${verilate-src "--cc --build --exe ./simulation/tester.cpp ./simulation/test_alu.cpp -top alu"}
|
||||||
mv obj_dir "$out"
|
mv obj_dir "$out"
|
||||||
mkdir "$out/bin" && cp "$out/Valu" "$out/bin/alu-sim"
|
mkdir "$out/bin" && cp "$out/Valu" "$out/bin/alu-sim"
|
||||||
'';
|
'';
|
||||||
|
|
||||||
alu-synth = pkgs.runCommandCC "alu-synth" {} ''
|
alu-synth = pkgs.runCommandCC "alu-synth" {} ''
|
||||||
mkdir -p "$out"
|
mkdir -p "$out"
|
||||||
find ${./verilog-src} -name '*.v' -exec ${yosys}/bin/yosys -Q -p "synth_ice40 -top topmost -json $out/synth.json -dsp" {} +
|
find ${./src} -name '*.v' -exec ${yosys}/bin/yosys -Q -p "synth_ice40 -top topmost -json $out/synth.json -dsp" {} +
|
||||||
'';
|
'';
|
||||||
|
|
||||||
alu-synth-view = pkgs.writeScriptBin "alu-synth-view" ''
|
alu-synth-view = pkgs.writeScriptBin "alu-synth-view" ''
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
#include "Valu.h"
|
#include "Valu.h"
|
||||||
#include "verilated.h"
|
#include "verilated.h"
|
||||||
#include "tester.hpp"
|
#include "tester.hpp"
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <random>
|
||||||
|
|
||||||
struct alu_testcase {
|
struct alu_testcase {
|
||||||
std::string name;
|
std::string name;
|
||||||
|
@ -16,6 +18,18 @@ struct alu_testcase {
|
||||||
std::optional<unsigned int> max_cycles;
|
std::optional<unsigned int> max_cycles;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
std::string fmt_hex(uint32_t n) {
|
||||||
|
char hex[100];
|
||||||
|
if (n < 0x100)
|
||||||
|
snprintf(hex, sizeof hex, "0x%02x", n);
|
||||||
|
else if (n < 0x10000)
|
||||||
|
snprintf(hex, sizeof hex, "0x%04x", n);
|
||||||
|
else
|
||||||
|
snprintf(hex, sizeof hex, "0x%08x", n);
|
||||||
|
|
||||||
|
return hex;
|
||||||
|
}
|
||||||
|
|
||||||
void test_op(Valu *valu, Tester *tester, alu_testcase test) {
|
void test_op(Valu *valu, Tester *tester, alu_testcase test) {
|
||||||
Tester subtester(tester, test.name);
|
Tester subtester(tester, test.name);
|
||||||
|
|
||||||
|
@ -26,13 +40,9 @@ void test_op(Valu *valu, Tester *tester, alu_testcase test) {
|
||||||
|
|
||||||
valu->eval();
|
valu->eval();
|
||||||
|
|
||||||
char o_name[100];
|
std::string o_name("O == ");
|
||||||
if (test.O < 0x100)
|
o_name.append(fmt_hex(test.O));
|
||||||
snprintf(o_name, sizeof o_name, "O == 0x%02x", test.O);
|
|
||||||
if (test.O < 0x10000)
|
|
||||||
snprintf(o_name, sizeof o_name, "O == 0x%04x", test.O);
|
|
||||||
else
|
|
||||||
snprintf(o_name, sizeof o_name, "O == 0x%08x", test.O);
|
|
||||||
subtester.assert_eq(o_name, valu->O, test.O);
|
subtester.assert_eq(o_name, valu->O, test.O);
|
||||||
|
|
||||||
if (test.overflow.has_value()) {
|
if (test.overflow.has_value()) {
|
||||||
|
@ -165,4 +175,92 @@ int main(int argc, char **argv) {
|
||||||
.O = 0x0, .zero = true,
|
.O = 0x0, .zero = true,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
Tester auto_t(&alu_t, "auto", true);
|
||||||
|
|
||||||
|
std::default_random_engine eng;
|
||||||
|
std::uniform_int_distribution<uint32_t> op_gen(0, 5);
|
||||||
|
std::uniform_int_distribution<uint32_t> gen(0, 0xffffffff);
|
||||||
|
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
uint32_t A = gen(eng);
|
||||||
|
uint32_t B = gen(eng);
|
||||||
|
std::string name;
|
||||||
|
|
||||||
|
switch (op_gen(eng)) {
|
||||||
|
case 0: // Add
|
||||||
|
name.append(fmt_hex(A));
|
||||||
|
name.append("+");
|
||||||
|
name.append(fmt_hex(B));
|
||||||
|
|
||||||
|
test_op(valu, &auto_t, {
|
||||||
|
.name = name,
|
||||||
|
.A = A, .B = B, .op = 0b000,
|
||||||
|
.O = A + B, .overflow = (A + B < A), .zero = (A + B == 0),
|
||||||
|
});
|
||||||
|
|
||||||
|
break;
|
||||||
|
case 1: // Subtract
|
||||||
|
name.append(fmt_hex(A));
|
||||||
|
name.append("-");
|
||||||
|
name.append(fmt_hex(B));
|
||||||
|
|
||||||
|
test_op(valu, &auto_t, {
|
||||||
|
.name = name,
|
||||||
|
.A = A, .B = B, .op = 0b001,
|
||||||
|
.O = A - B, .overflow = (B > A), .zero = (A == B),
|
||||||
|
});
|
||||||
|
|
||||||
|
break;
|
||||||
|
case 2: // Bitwise AND
|
||||||
|
name.append(fmt_hex(A));
|
||||||
|
name.append("&");
|
||||||
|
name.append(fmt_hex(B));
|
||||||
|
|
||||||
|
test_op(valu, &auto_t, {
|
||||||
|
.name = name,
|
||||||
|
.A = A, .B = B, .op = 0b100,
|
||||||
|
.O = A & B, .overflow = 0, .zero = ((A & B) == 0),
|
||||||
|
});
|
||||||
|
|
||||||
|
break;
|
||||||
|
case 3: // Bitwise OR
|
||||||
|
name.append(fmt_hex(A));
|
||||||
|
name.append("|");
|
||||||
|
name.append(fmt_hex(B));
|
||||||
|
|
||||||
|
test_op(valu, &auto_t, {
|
||||||
|
.name = name,
|
||||||
|
.A = A, .B = B, .op = 0b101,
|
||||||
|
.O = A | B, .overflow = 0, .zero = ((A | B) == 0),
|
||||||
|
});
|
||||||
|
|
||||||
|
break;
|
||||||
|
case 4: // Bitwise XOR
|
||||||
|
name.append(fmt_hex(A));
|
||||||
|
name.append("^");
|
||||||
|
name.append(fmt_hex(B));
|
||||||
|
|
||||||
|
test_op(valu, &auto_t, {
|
||||||
|
.name = name,
|
||||||
|
.A = A, .B = B, .op = 0b110,
|
||||||
|
.O = A ^ B, .overflow = 0, .zero = ((A ^ B) == 0),
|
||||||
|
});
|
||||||
|
|
||||||
|
break;
|
||||||
|
case 5: // Bitwise NOT
|
||||||
|
name.append("~");
|
||||||
|
name.append(fmt_hex(A));
|
||||||
|
|
||||||
|
test_op(valu, &auto_t, {
|
||||||
|
.name = name,
|
||||||
|
.A = A, .B = B, .op = 0b111,
|
||||||
|
.O = ~A, .overflow = 0, .zero = (A == 0xffffffff),
|
||||||
|
});
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,12 +23,16 @@ module alu(
|
||||||
wire [31:0] adder_out;
|
wire [31:0] adder_out;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
|
wire addition = op == 3'b000;
|
||||||
wire subtraction = op == 3'b001;
|
wire subtraction = op == 3'b001;
|
||||||
wire [31:0] adder_B = subtraction ? ~B : B;
|
wire [31:0] adder_B = subtraction ? ~B : B;
|
||||||
wire adder_cout;
|
wire adder_cout;
|
||||||
|
|
||||||
carry_select_adder a(A, adder_B, subtraction, adder_out, adder_cout);
|
carry_select_adder a(A, adder_B, subtraction, adder_out, adder_cout);
|
||||||
assign Fflow = subtraction ? ~adder_cout : adder_cout;
|
assign Fflow =
|
||||||
|
addition ? adder_cout :
|
||||||
|
subtraction ? ~adder_cout :
|
||||||
|
0;
|
||||||
end
|
end
|
||||||
|
|
||||||
wire [31:0] mult_out_hi;
|
wire [31:0] mult_out_hi;
|
Loading…
Reference in New Issue
Block a user