Skip to content

Commit

Permalink
0.2.rev.A. Add tests to devices
Browse files Browse the repository at this point in the history
  • Loading branch information
robsonsmartins committed Jan 4, 2024
1 parent 7c933cc commit c359114
Show file tree
Hide file tree
Showing 12 changed files with 1,356 additions and 2 deletions.
8 changes: 8 additions & 0 deletions software/usbflashprog/backend/devices/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
#include <QString>
#include <QByteArray>

#ifndef TEST_BUILD
#include "backend/runner.hpp"
#else
#include "test/emulator/emulator.hpp"
#endif

// ---------------------------------------------------------------------------

Expand Down Expand Up @@ -336,7 +340,11 @@ class Device : public QObject {
/* @brief Serial port path. */
QString port_;
/* @brief The Runner instance. */
#ifndef TEST_BUILD
Runner runner_;
#else
Emulator runner_;
#endif
/* @brief Device information. */
TDeviceInformation info_;
};
Expand Down
20 changes: 18 additions & 2 deletions software/usbflashprog/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,27 @@ set(PROJECT_SOURCES
../backend/opcodes.hpp
../backend/runner.cpp
../backend/runner.hpp
../backend/devices/device.hpp
../backend/devices/device.cpp
../backend/devices/parallel/sram.hpp
../backend/devices/parallel/sram.cpp
../backend/devices/parallel/eprom.hpp
../backend/devices/parallel/eprom.cpp
mock/qserialport.hpp
backend/opcodes_test.hpp
backend/opcodes_test.cpp
emulator/emulator.cpp
emulator/emulator.hpp
emulator/chip.cpp
emulator/chip.hpp
emulator/sram.cpp
emulator/sram.hpp
emulator/eprom.cpp
emulator/eprom.hpp
backend/chip_test.hpp
backend/chip_test.cpp
backend/runner_test.hpp
backend/runner_test.cpp
backend/opcodes_test.hpp
backend/opcodes_test.cpp
main.cpp
)

Expand Down
155 changes: 155 additions & 0 deletions software/usbflashprog/test/backend/chip_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// ---------------------------------------------------------------------------
// USB EPROM/Flash Programmer
//
// Copyright (2024) Robson Martins
//
// This work is licensed under a Creative Commons Attribution-NonCommercial-
// ShareAlike 4.0 International License.
// ---------------------------------------------------------------------------
/**
* @ingroup UnitTests
* @file test/backend/chip_test.cpp
* @brief Implementation of Unit Test for Device (Chip) Classes.
*
* @author Robson Martins (https://www.robsonmartins.com)
*/
// ---------------------------------------------------------------------------

#include <QByteArray>
#include <QObject>
#include <cmath>

#include "chip_test.hpp"

#include "emulator/emulator.hpp"
#include "emulator/sram.hpp"
#include "emulator/eprom.hpp"

#include "../../backend/devices/parallel/sram.hpp"
#include "../../backend/devices/parallel/eprom.hpp"

// ---------------------------------------------------------------------------

/* GTest macro to print to console */
#define GTEST_COUT std::cerr << "[ ] ChipTest current: "

// ---------------------------------------------------------------------------
// private functions

/* Run the chip tests.
@param emuChip Pointer to Chip Emulator object.
@param device Pointer to Device object to test.
@param size Size of the device, in bytes.
*/
void runChipTests(BaseChip *emuChip, Device *device, uint32_t size);

// ---------------------------------------------------------------------------

TEST_F(ChipTest, sram_test) {
ChipSRAM *emuChip = new ChipSRAM();
Emulator::setChip(emuChip);
SRAM *device = new SRAM();
runChipTests(emuChip, device, 2048);
runChipTests(emuChip, device, 8192);
delete device;
delete emuChip;
}

TEST_F(ChipTest, eprom27_test) {
ChipEPROM *emuChip = new ChipEPROM();
Emulator::setChip(emuChip);
M27xxx *device = new M27xxx();
runChipTests(emuChip, device, 2048);
runChipTests(emuChip, device, 8192);
delete emuChip;
delete device;
}

TEST_F(ChipTest, eprom27C_test) {
ChipEPROM *emuChip = new ChipEPROM();
Emulator::setChip(emuChip);
M27Cxxx *device = new M27Cxxx();
runChipTests(emuChip, device, 2048);
runChipTests(emuChip, device, 8192);
delete emuChip;
delete device;
}

TEST_F(ChipTest, eprom27C16Bit_test) {
ChipEPROM *emuChip = new ChipEPROM();
Emulator::setChip(emuChip);
M27C16Bit *device = new M27C16Bit();
runChipTests(emuChip, device, 2048);
runChipTests(emuChip, device, 8192);
delete emuChip;
delete device;
}

TEST_F(ChipTest, epromW27E_test) {
ChipEPROM *emuChip = new ChipEPROM();
Emulator::setChip(emuChip);
W27Exxx *device = new W27Exxx();
runChipTests(emuChip, device, 2048);
runChipTests(emuChip, device, 8192);
delete emuChip;
delete device;
}

// ---------------------------------------------------------------------------

void runChipTests(BaseChip *emuChip, Device *device, uint32_t size) {
QByteArray buffer;
device->setPort("COM1");
emuChip->setSize(size);
device->setSize(size);
Emulator::randomizeBuffer(buffer, size);

TDeviceCapabilities cap = device->getInfo().capability;

if (cap.hasBlankCheck) {
GTEST_COUT << "Blank Check" << std::endl;
EXPECT_EQ(device->blankCheck(), true);
}
if (cap.hasProgram) {
GTEST_COUT << "Program" << std::endl;
EXPECT_EQ(device->program(buffer), true);
if (cap.hasVerify) {
GTEST_COUT << "Verify" << std::endl;
EXPECT_EQ(device->verify(buffer), true);
}
}
if (cap.hasProgram && cap.hasVerify) {
GTEST_COUT << "Program and Verify" << std::endl;
EXPECT_EQ(device->program(buffer, true), true);
}
if (cap.hasProgram && cap.hasBlankCheck) {
GTEST_COUT << "Blank Check" << std::endl;
EXPECT_EQ(device->blankCheck(), false);
}
if (cap.hasProgram && cap.hasVerify) {
buffer[buffer.size() - 1] = ~buffer[buffer.size() - 1];
GTEST_COUT << "Verify" << std::endl;
EXPECT_EQ(device->verify(buffer), false);
}
if (cap.hasErase) {
GTEST_COUT << "Erase" << std::endl;
EXPECT_EQ(device->erase(), true);
if (cap.hasBlankCheck) {
GTEST_COUT << "Blank Check" << std::endl;
EXPECT_EQ(device->blankCheck(), true);
}
if (cap.hasVerify) {
buffer[buffer.size() - 1] = ~buffer[buffer.size() - 1];
GTEST_COUT << "Verify" << std::endl;
EXPECT_EQ(device->verify(buffer), false);
}
if (cap.hasProgram) {
GTEST_COUT << "Program" << std::endl;
EXPECT_EQ(device->program(buffer), true);
}
if (cap.hasBlankCheck) {
GTEST_COUT << "Erase and Blank Check" << std::endl;
EXPECT_EQ(device->erase(true), true);
}
}
}
43 changes: 43 additions & 0 deletions software/usbflashprog/test/backend/chip_test.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// ---------------------------------------------------------------------------
// USB EPROM/Flash Programmer
//
// Copyright (2024) Robson Martins
//
// This work is licensed under a Creative Commons Attribution-NonCommercial-
// ShareAlike 4.0 International License.
// ---------------------------------------------------------------------------
/**
* @ingroup UnitTests
* @file test/backend/chip_test.hpp
* @brief Header of Unit Test for Device (Chip) Classes.
*
* @author Robson Martins (https://www.robsonmartins.com)
*/
// ---------------------------------------------------------------------------

#ifndef TEST_BACKEND_CHIP_TEST_HPP_
#define TEST_BACKEND_CHIP_TEST_HPP_

#include <gtest/gtest.h>

// ---------------------------------------------------------------------------

/**
* @ingroup UnitTests
* @brief Test class for Device (Chip) Classes.
* @details The purpose of this class is to test the Device (Chip) Classes.
* @nosubgrouping
*/
class ChipTest : public testing::Test {
protected:
/** @brief Constructor. */
ChipTest() {}
/** @brief Destructor. */
~ChipTest() override {}
/** @brief Sets Up the test. */
void SetUp() override {}
/** @brief Teardown of the test. */
void TearDown() override {}
};

#endif // TEST_BACKEND_CHIP_TEST_HPP_
144 changes: 144 additions & 0 deletions software/usbflashprog/test/emulator/chip.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// ---------------------------------------------------------------------------
// USB EPROM/Flash Programmer
//
// Copyright (2024) Robson Martins
//
// This work is licensed under a Creative Commons Attribution-NonCommercial-
// ShareAlike 4.0 International License.
// ---------------------------------------------------------------------------
/**
* @ingroup UnitTests
* @file test/emulator/chip.cpp
* @brief Implementation of Chip Emulation Base Classes.
*
* @author Robson Martins (https://www.robsonmartins.com)
*/
// ---------------------------------------------------------------------------

#include <QRandomGenerator>
#include "chip.hpp"

// ---------------------------------------------------------------------------

BaseChip::BaseChip()
: f_vdd(false), f_vpp(false), f_addr_bus(0), f_data_bus(0) {}

BaseChip::BaseChip(const BaseChip& src)
: f_vdd(src.f_vdd),
f_vpp(src.f_vpp),
f_addr_bus(src.f_addr_bus),
f_data_bus(src.f_data_bus) {
f_memory_area = src.f_memory_area;
}

BaseChip& BaseChip::operator=(const BaseChip& src) {
f_vdd = src.f_vdd;
f_vpp = src.f_vpp;
f_addr_bus = src.f_addr_bus;
f_data_bus = src.f_data_bus;
f_memory_area = src.f_memory_area;
return *this;
}

BaseChip::~BaseChip() {}

void BaseChip::setSize(uint32_t size) {
if (size == f_memory_area.size()) return;
/* sets the chip size */
f_memory_area.resize(size);
}

void BaseChip::setVDD(bool state) {
if (state == f_vdd) return;
f_vdd = state;
emuChip();
}

void BaseChip::setVPP(bool state) {
if (state == f_vpp) return;
f_vpp = state;
emuChip();
}

void BaseChip::randomizeData(void) {
/* fills memory area with random data */
for (int i = 0; i < f_memory_area.size(); ++i) {
f_memory_area[i] =
static_cast<uint16_t>(QRandomGenerator::global()->generate());
}
}

void BaseChip::fillData(uint16_t data) {
/* fills memory area with specified data */
std::fill(f_memory_area.begin(), f_memory_area.end(), data);
}

void BaseChip::read(void) {
/* checks the params */
if (f_addr_bus >= f_memory_area.size()) {
f_data_bus = 0xFFFF;
return;
}
static uint32_t last_addr = f_addr_bus;
uint16_t data = f_memory_area[f_addr_bus];
if (f_data_bus == data && f_addr_bus == last_addr) return;
/* returns the data from memory area */
f_data_bus = data;
/* update last_addr */
last_addr = f_addr_bus;
}

void BaseChip::write(void) {
/* checks the params */
if (f_addr_bus >= f_memory_area.size()) return;
/* writes the data to memory area */
f_memory_area[f_addr_bus] = f_data_bus;
}

// ---------------------------------------------------------------------------

BaseParChip::BaseParChip()
: BaseChip(), f_oe(false), f_ce(false), f_we(false) {}

BaseParChip::~BaseParChip() {}

void BaseParChip::setOE(bool state) {
if (state == f_oe) return;
f_oe = state;
emuChip();
}

void BaseParChip::setCE(bool state) {
if (state == f_ce) return;
f_ce = state;
emuChip();
}

void BaseParChip::setWE(bool state) {
if (state == f_we) return;
f_we = state;
emuChip();
}

void BaseParChip::setAddrBus(uint32_t addr) {
/* calculates the valid address (into memory area) */
uint32_t addr_new = (addr & (f_memory_area.size() - 1));
if (f_addr_bus == addr_new) return;
/* set addr bus */
f_addr_bus = addr_new;
}

void BaseParChip::setDataBus(uint16_t data) {
if (f_data_bus == data) return;
/* changes the data bus */
f_data_bus = data;
/* writes data */
emuChip();
}

uint16_t BaseParChip::getDataBus(void) {
/* reads data */
emuChip();
/* returns the data bus */
return f_data_bus;
}
Loading

0 comments on commit c359114

Please sign in to comment.