2024-01-12 13:44:33 +01:00
|
|
|
#include "Valu.h"
|
|
|
|
#include "verilated.h"
|
2024-01-12 22:32:45 +01:00
|
|
|
#include "tester.hpp"
|
2024-01-12 20:45:05 +01:00
|
|
|
|
2024-01-12 13:44:33 +01:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
VerilatedContext *vctx = new VerilatedContext;
|
|
|
|
Valu *valu = new Valu(vctx);
|
2024-01-12 20:45:05 +01:00
|
|
|
|
2024-01-12 22:32:45 +01:00
|
|
|
Tester alu_t("alu");
|
2024-01-12 20:45:05 +01:00
|
|
|
{
|
2024-01-12 22:32:45 +01:00
|
|
|
Tester add_t(&alu_t, "add");
|
|
|
|
|
|
|
|
{
|
|
|
|
Tester add_case(&add_t, "0x2137+0x1234");
|
|
|
|
valu->op = 0b000;
|
|
|
|
valu->A = 0x2137;
|
|
|
|
valu->B = 0x1234;
|
|
|
|
valu->eval();
|
|
|
|
add_case.assert_eq("O == 0x336b", valu->O, 0x336b);
|
|
|
|
add_case.assert_eq("no overflow", valu->Fflow, 0x0);
|
|
|
|
add_case.assert_eq("no Fzero", valu->Fzero, 0x0);
|
|
|
|
}
|
2024-01-12 20:45:05 +01:00
|
|
|
|
2024-01-12 22:32:45 +01:00
|
|
|
{
|
|
|
|
Tester add_case(&add_t, "0x9+0x10");
|
|
|
|
valu->op = 0b000;
|
|
|
|
valu->A = 0x9;
|
|
|
|
valu->B = 0x10;
|
|
|
|
valu->eval();
|
|
|
|
add_case.assert_eq("O == 0x19", valu->O, 0x19);
|
|
|
|
add_case.assert_eq("no overflow", valu->Fflow, 0x0);
|
2024-01-12 23:08:18 +01:00
|
|
|
add_case.assert_eq("no Fzero", valu->Fzero, 0x0);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
Tester add_case(&add_t, "0x5555+0x5555");
|
|
|
|
valu->op = 0b000;
|
|
|
|
valu->A = 0x5555;
|
|
|
|
valu->B = 0x5555;
|
|
|
|
valu->eval();
|
|
|
|
add_case.assert_eq("O == 0xaaaa", valu->O, 0xaaaa);
|
|
|
|
add_case.assert_eq("no overflow", valu->Fflow, 0x0);
|
2024-01-12 22:32:45 +01:00
|
|
|
add_case.assert_eq("no Fzero", valu->Fzero, 0x0);
|
|
|
|
}
|
2024-01-12 20:45:05 +01:00
|
|
|
|
2024-01-12 22:32:45 +01:00
|
|
|
{
|
|
|
|
Tester add_case(&add_t, "0xffffffff+0x1");
|
|
|
|
valu->op = 0b000;
|
|
|
|
valu->A = 0xffffffff;
|
|
|
|
valu->B = 0x1;
|
|
|
|
valu->eval();
|
|
|
|
add_case.assert_eq("O == 0x0", valu->O, 0x0);
|
|
|
|
add_case.assert_eq("has overflow", valu->Fflow, 0x1);
|
|
|
|
add_case.assert_eq("has Fzero", valu->Fzero, 0x1);
|
|
|
|
}
|
2024-01-12 20:45:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2024-01-12 22:32:45 +01:00
|
|
|
Tester sub_t(&alu_t, "sub");
|
|
|
|
|
|
|
|
{
|
|
|
|
Tester sub_case(&sub_t, "0x2137-0x0420");
|
|
|
|
valu->op = 0b001;
|
|
|
|
valu->A = 0x2137;
|
|
|
|
valu->B = 0x0420;
|
|
|
|
valu->eval();
|
|
|
|
sub_case.assert_eq("O == 0x1d17", valu->O, 0x1d17);
|
|
|
|
sub_case.assert_eq("no underflow", valu->Fflow, 0x0);
|
|
|
|
sub_case.assert_eq("no Fzero", valu->Fzero, 0x0);
|
|
|
|
}
|
2024-01-12 20:45:05 +01:00
|
|
|
|
2024-01-12 22:32:45 +01:00
|
|
|
{
|
|
|
|
Tester sub_case(&sub_t, "0x100-0x200");
|
|
|
|
valu->op = 0b001;
|
|
|
|
valu->A = 0x100;
|
|
|
|
valu->B = 0x200;
|
|
|
|
valu->eval();
|
|
|
|
sub_case.assert_eq("O == 0xffffff00", valu->O, 0xffffff00);
|
|
|
|
sub_case.assert_eq("has underflow", valu->Fflow, 0x1);
|
|
|
|
sub_case.assert_eq("no Fzero", valu->Fzero, 0x0);
|
|
|
|
}
|
2024-01-12 20:45:05 +01:00
|
|
|
|
2024-01-12 22:32:45 +01:00
|
|
|
{
|
|
|
|
Tester sub_case(&sub_t, "0x0-0x1");
|
|
|
|
valu->op = 0b001;
|
|
|
|
valu->A = 0x0;
|
|
|
|
valu->B = 0x1;
|
|
|
|
valu->eval();
|
|
|
|
sub_case.assert_eq("O == 0xffffffff", valu->O, 0xffffffff);
|
|
|
|
sub_case.assert_eq("has underflow", valu->Fflow, 0x1);
|
|
|
|
sub_case.assert_eq("no Fzero", valu->Fzero, 0x0);
|
|
|
|
}
|
2024-01-12 20:45:05 +01:00
|
|
|
|
2024-01-12 22:32:45 +01:00
|
|
|
{
|
|
|
|
Tester sub_case(&sub_t, "0x20-0x20");
|
|
|
|
valu->op = 0b001;
|
|
|
|
valu->A = 0x20;
|
|
|
|
valu->B = 0x20;
|
|
|
|
valu->eval();
|
|
|
|
sub_case.assert_eq("O == 0x0", valu->O, 0x0);
|
|
|
|
sub_case.assert_eq("no underflow", valu->Fflow, 0x0);
|
|
|
|
sub_case.assert_eq("has Fzero", valu->Fzero, 0x1);
|
|
|
|
}
|
2024-01-12 20:45:05 +01:00
|
|
|
}
|
|
|
|
|
2024-01-13 00:16:28 +01:00
|
|
|
{
|
|
|
|
Tester bitwise_t(&alu_t, "bitwise");
|
|
|
|
// 0x3 = 0b0011, 0x5 = 0b0101
|
|
|
|
{
|
|
|
|
Tester and_case(&bitwise_t, "0x3&0x5");
|
|
|
|
valu->op = 0b100;
|
|
|
|
valu->A = 0x3;
|
|
|
|
valu->B = 0x5;
|
|
|
|
valu->eval();
|
|
|
|
and_case.assert_eq("O == 0x1", valu->O, 0x1);
|
|
|
|
and_case.assert_eq("no Fzero", valu->Fzero, 0x0);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
Tester or_case(&bitwise_t, "0x3|0x5");
|
|
|
|
valu->op = 0b101;
|
|
|
|
valu->A = 0x3;
|
|
|
|
valu->B = 0x5;
|
|
|
|
valu->eval();
|
|
|
|
or_case.assert_eq("O == 0x7", valu->O, 0x7);
|
|
|
|
or_case.assert_eq("no Fzero", valu->Fzero, 0x0);
|
|
|
|
}
|
2024-01-12 20:45:05 +01:00
|
|
|
|
2024-01-13 00:16:28 +01:00
|
|
|
{
|
|
|
|
Tester xor_case(&bitwise_t, "0x3^0x5");
|
|
|
|
valu->op = 0b110;
|
|
|
|
valu->A = 0x3;
|
|
|
|
valu->B = 0x5;
|
|
|
|
valu->eval();
|
|
|
|
xor_case.assert_eq("O == 0x6", valu->O, 0x6);
|
|
|
|
xor_case.assert_eq("no Fzero", valu->Fzero, 0x0);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
Tester xor_case(&bitwise_t, "~0xa5a5a5a5");
|
|
|
|
valu->op = 0b111;
|
|
|
|
valu->A = 0xa5a5a5a5;
|
|
|
|
valu->eval();
|
|
|
|
xor_case.assert_eq("O == 0x5a5a5a5a", valu->O, 0x5a5a5a5a);
|
|
|
|
xor_case.assert_eq("no Fzero", valu->Fzero, 0x0);
|
|
|
|
}
|
|
|
|
}
|
2024-01-12 13:44:33 +01:00
|
|
|
}
|