Skip to content

Commit

Permalink
Fix stack pointing to last-1 instead of the last push according to #10
Browse files Browse the repository at this point in the history
  • Loading branch information
FanisDeligiannis committed Apr 13, 2023
1 parent 4859137 commit edc7bc0
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions 8085_emu/include/stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace InternalEmulator
//Simple stack.
//uint8_t* _Data SHOULD point to the _Data of Memory.

#define STACK_START 0xFFFE

class Stack
{
private:
Expand All @@ -22,42 +24,51 @@ namespace InternalEmulator
_Size = (int)pow(2, bits);
_Data = nullptr;
_SP = std::shared_ptr<uint16_t>((uint16_t*)calloc(1, sizeof(uint16_t)), free);
*(_SP.get()) = _Size - 1;
//*(_SP.get()) = _Size - 1;
*(_SP.get()) = STACK_START;
}

~Stack() {}

void SetDataPointer(std::shared_ptr<uint8_t> data)
inline void SetDataPointer(std::shared_ptr<uint8_t> data)
{
_Data = data;
}

void Push(uint8_t data)
inline void Push(uint8_t data)
{
_Data.get()[*(_SP.get())] = data;
(*(_SP.get()))--;
_Data.get()[*(_SP.get())] = data;
}

uint8_t Pop()
inline uint8_t Pop()
{
(*(_SP.get()))++;
uint8_t ret = _Data.get()[*(_SP.get())];
_Data.get()[*(_SP.get())] = 0;
(*(_SP.get()))++;

if (*(_SP.get()) > STACK_START)
{
//ERROR!
#ifndef _DIST //temporary. Visible in the console, if you're on release/debug mode.
printf("%s\n", "ERROR: POP on empty stack occured!");
#endif
}

return ret;
}

uint16_t GetSP()
inline uint16_t GetSP()
{
return *(_SP.get());
}

std::shared_ptr<uint16_t> GetSPPointer()
inline std::shared_ptr<uint16_t> GetSPPointer()
{
return _SP;
}

std::shared_ptr<uint8_t> GetData()
inline std::shared_ptr<uint8_t> GetData()
{
return _Data;
}
Expand Down

0 comments on commit edc7bc0

Please sign in to comment.