Skip to content

Commit

Permalink
[#9] fpu barebones
Browse files Browse the repository at this point in the history
  • Loading branch information
laudominik committed Sep 19, 2023
1 parent 1e0f856 commit 4707b06
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/basicComponents/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
target_sources(basic_emu PUBLIC ${SOURCE_DIR}/basicComponents/Cpu.cpp)
target_sources(basic_emu PUBLIC
${SOURCE_DIR}/basicComponents/Cpu.cpp
${SOURCE_DIR}/basicComponents/Fpu.cpp)
13 changes: 13 additions & 0 deletions src/basicComponents/Fpu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <Fpu.h>

RegisterFpu& Fpu::st(std::size_t index){
return stack[stack.size() - index - 1];
}

bool Fpu::push(RegisterFpu val){
if(stack.size() == numRegisters) {
return false;
}
stack.push_back(val);
return true;
}
15 changes: 15 additions & 0 deletions src/basicComponents/Fpu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <vector>

#include <Register.h>

class Fpu {
public:
RegisterFpu& st(std::size_t index);
bool push(RegisterFpu val);

private:
static constexpr auto numRegisters = 8;
std::vector<RegisterFpu> stack;
};
37 changes: 36 additions & 1 deletion src/basicComponents/Register.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,41 @@ class Register {
std::string mnemonic;
};

class RegisterFpu {
public:
template<typename PRECISION_T>
inline void set(PRECISION_T v){
static_assert(false, "access to Fpu register should be either by float or double");
}

template<typename PRECISION_T>
inline PRECISION_T get(){
static_assert(false, "access to Fpu register should be either by float or double");
}

private:
double value{};
};

template<>
inline double RegisterFpu::get(){
return value;
}

template<>
inline float RegisterFpu::get(){
return static_cast<float>(value);
}

template<>
inline void RegisterFpu::set(double v){
value = v;
}

template<>
inline void RegisterFpu::set(float v){
value = v;
}

using Register32 = Register<Dword>;
using Register16 = Register<Word>;

1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ add_executable(emulator_test
memory.cpp
register.cpp
status.cpp
fpu.cpp
InstructionDecoderTest.cpp
)

Expand Down
29 changes: 29 additions & 0 deletions test/fpu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <gtest/gtest.h>

#include <basicComponents/Fpu.h>

TEST(FpuTest, testStAccess){
Fpu fpu;
RegisterFpu sth;
sth.set<double>(1.0);
fpu.push(sth);
EXPECT_DOUBLE_EQ(fpu.st(0).get<double>(), 1.0);
sth.set<float>(2.f);
fpu.push(sth);
EXPECT_DOUBLE_EQ(fpu.st(0).get<double>(), 2.0);
EXPECT_DOUBLE_EQ(fpu.st(1).get<double>(), 1.0);
sth.set<double>(3.0);
fpu.push(sth);
EXPECT_DOUBLE_EQ(fpu.st(0).get<double>(), 3.0);
EXPECT_DOUBLE_EQ(fpu.st(1).get<double>(), 2.0);
EXPECT_DOUBLE_EQ(fpu.st(2).get<double>(), 1.0);
}

TEST(FpuTest, testFpuStackOverflow){
Fpu fpu;
for(size_t i = 0u ; i < 8; i++){
EXPECT_TRUE(fpu.push(RegisterFpu()));
}
EXPECT_FALSE(fpu.push(RegisterFpu()));
}

0 comments on commit 4707b06

Please sign in to comment.