Skip to content

Commit

Permalink
two's complement converting function
Browse files Browse the repository at this point in the history
  • Loading branch information
tnibert committed Nov 5, 2019
1 parent 9cb3b69 commit 0cc6476
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
19 changes: 19 additions & 0 deletions system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ uint16_t revlendianbytes(uint8_t lsig, uint8_t msig)
return offset;
}

/**
* Convert a two's comp value to regular signed number
* @param in
* @return
*/
int8_t decode_twos_comp(uint8_t in)
{
// if not two's comp, return the original number
if((in & 0b10000000) == 0b0)
{
return in;
}
else
{
uint8_t minusone = in - 0b1;
return -(~minusone);
}
}

cpustate * initcpu()
{
// initialize cpu state
Expand Down
12 changes: 12 additions & 0 deletions tests/systemtests.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,17 @@ class TestDecoder : public CppUnit::TestFixture

CPPUNIT_TEST_SUITE_REGISTRATION( TestDecoder );

class TestUtilFuncs : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( TestUtilFuncs );
CPPUNIT_TEST( test_decode_twos_comp );
CPPUNIT_TEST_SUITE_END();

public:
void test_decode_twos_comp();
};

CPPUNIT_TEST_SUITE_REGISTRATION( TestUtilFuncs );

#endif //OPENNES_SYSTEMTESTS_H

13 changes: 13 additions & 0 deletions tests/testutilfuncs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "systemtests.h"
#include <bitset>
using namespace CppUnit;


void TestUtilFuncs::test_decode_twos_comp()
{
// todo: assert fails
//CPPUNIT_ASSERT( 0b10101011 == -85 );
//std::cout << std::bitset<8>(decode_twos_comp(0b10101011)) << std::endl;
CPPUNIT_ASSERT(decode_twos_comp(0b10101011) == -85);
CPPUNIT_ASSERT(decode_twos_comp(0b01010101) == 85);
}

0 comments on commit 0cc6476

Please sign in to comment.