Skip to content

Commit

Permalink
Migrate PSEMU to use error codes in Logging Class Error Function
Browse files Browse the repository at this point in the history
  • Loading branch information
Cherrytree56567 committed Sep 22, 2023
1 parent abc6bb1 commit c90c68f
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 65 deletions.
11 changes: 6 additions & 5 deletions PSEMU/CPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ void CPU::op_add(uint32_t instruction) {

if (rt > 32) {
Logging console;
console.err("Overflow Exception: RT is greater than 32. RT = " + std::to_string(rt));
// Overflow Exception: RT is greater than 32.
console.err(51);
} else {
std::cout << "ADDING: RESULT = " << registers.reg[rs] + registers.reg[rt] << ", RS = " << std::to_string(rs) << ", RT = " << std::to_string(rt) << ", RD = " << std::to_string(rd) << std::endl;

registers.reg[rd] = registers.reg[rs] + registers.reg[rt];
}
}

// First take the data from RT (Register) and stores it in IMM + RS (memory)
// First take the data from RT (Register) and store it in IMM + RS (memory)

void CPU::op_storebyte(uint32_t instruction) {
uint8_t rs = (instruction >> 21) & 0x1F; // Extract bits 25 to 21
Expand Down Expand Up @@ -74,7 +75,7 @@ void CPU::op_addi(uint32_t instruction) {

if (rt > 32) {
Logging console;
console.err("Overflow Exception: RT is greater than 32. RT = " + std::to_string(rt));
console.err(51);
} else {
registers.reg[rt] = imm + registers.reg[rs];

Expand Down Expand Up @@ -596,14 +597,14 @@ void CPU::loadBIOS(const char* filename) {
// Allocate memory for the array
uint32_t* aBiosCode = (uint32_t*)malloc(numChunks * sizeof(uint32_t));
if (!aBiosCode) {
console.err("Memory allocation error\n");
console.err(52);
fclose(file);
}

// Read and separate the content into 32-bit chunks
size_t bytesRead = fread(aBiosCode, sizeof(uint32_t), numChunks, file);
if (bytesRead < numChunks) {
console.err("Error reading file\n");
console.err(53);
free(aBiosCode);
fclose(file);
}
Expand Down
3 changes: 2 additions & 1 deletion PSEMU/GPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class GPU {
return GPUmemory[address];
}
else {
std::cerr << "Memory access out of bounds: " << address << std::endl;
Logging Console;
Console.err(54);
// You might want to handle this error situation accordingly.
// For now, returning a reference to a static variable to indicate an error.
static uint8_t dummy_error_value = 0;
Expand Down
6 changes: 2 additions & 4 deletions PSEMU/Logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
*/
#include "Logging.h"

void Logging::err(std::string message) {
void Logging::err(int message) {
if (errstatus) {
system("Color 0A");
std::cout << "ERROR: " << message << std::endl;
system("Color 7A");
exit(message);
}
}

Expand Down
2 changes: 1 addition & 1 deletion PSEMU/Logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Logging
bool warnstatus = true;
bool errstatus = true;
bool infostatus = true;
void err(std::string message);
void err(int message);
void warn(std::string message);
void log(std::string message);
};
Expand Down
14 changes: 8 additions & 6 deletions PSEMU/Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ uint8_t Memory::readByte(uint32_t address) const {
}
else {
Logging console;
console.warn("Out of bounds memory access");
console.err(54);
return 0;
}
}
Expand All @@ -25,7 +25,7 @@ void Memory::writeByte(uint32_t address, uint8_t value) {
}
else {
Logging console;
console.warn("Out of bounds memory access");
console.err(54);
}
}

Expand All @@ -39,7 +39,7 @@ uint32_t Memory::readWord(uint32_t address) const {
}
else {
Logging console;
console.warn("Out of bounds memory access");
console.err(54);
return 0;
}
}
Expand All @@ -52,18 +52,20 @@ void Memory::writeWord(uint32_t address, uint32_t value) {
}
else {
Logging console;
console.warn("Out of bounds memory access");
console.err(54);
}
}

void Memory::writeHalfword(uint32_t address, uint16_t value) {
if (address % 2 != 0) {
std::cerr << "Error: Address must be halfword-aligned." << std::endl;
Logging console;
console.err(55);
return;
}

if (address >= memory.size()) {
std::cerr << "Error: Address out of range." << std::endl;
Logging console;
console.err(56);
return;
}

Expand Down
3 changes: 2 additions & 1 deletion PSEMU/Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class Memory {
} else if (address >= GPU_VRAM_START && address <= GPU_VRAM_END) {
return gpu[address - GPU_VRAM_START];
} else {
std::cerr << "Memory access out of bounds: " << address << std::endl;
Logging console;
console.err(54);
// You might want to handle this error situation accordingly.
// For now, returning a reference to a static variable to indicate an error.
static uint8_t dummy_error_value = 0;
Expand Down
56 changes: 9 additions & 47 deletions wiki/ERROR_CODE.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,10 @@
## Client Error Response
| Status Code | Message |
## Exit Error Codes
| Error Code | Message |
|-------------|--------------|
| 400 | Bad Request |
| 401 | Unauthorized |
| 402 | Payment Required |
| 403 | Forbidden |
| 404 | Not Found |
| 405 | Method Not Allowed |
| 406 | Not Acceptable |
| 407 | Proxy Authentication Required |
| 408 | Request Timeout |
| 409 | Conflict |
| 410 | Gone |
| 411 | Length Required |
| 412 | Precondition Failed |
| 413 | Payload Too Large |
| 414 | URI Too Long |
| 415 | Unsupported Media Type |
| 416 | Range Not Satisfiable |
| 417 | Expectation Failed |
| 418 | I'm a teapot |
| 421 | Misdirected Request |
| 422 | Unprocessable Content (WebDAV) |
| 423 | Locked (WebDAV) |
| 424 | Failed Dependency (WebDAV) |
| 425 | Too Early Experimental |
| 426 | Upgrade Required |
| 428 | Precondition Required |
| 429 | Too Many Requests |
| 431 | Request Header Fields Too Large |
| 451 | Unavailable For Legal Reasons |


## Server Error Response
| Status Code | Message |
|-------------|-----------|
| 500 | Internal Server Error |
| 501 | Not Implemented |
| 502 | Bad Gateway |
| 503 | Service Unavailable |
| 504 | Gateway Timeout |
| 505 | HTTP Version Not Supported |
| 506 | Variant Also Negotiates |
| 507 | Insufficient Storage (WebDAV) |
| 508 | Loop Detected (WebDAV) |
| 510 | Not Extended |
| 511 | Network Authentication Required |
| 51 | Overflow Exception: RT is greater than 32. |
| 52 | Memory allocation error. |
| 53 | Error reading file |
| 54 | Memory access out of bounds |
| 55 | Address must be halfword-aligned. |
| 56 | Address out of range. |
| 55 | Memory access out of bounds |

0 comments on commit c90c68f

Please sign in to comment.