Skip to content

Commit

Permalink
Merge pull request #94 from RadWolfie/option-output-video
Browse files Browse the repository at this point in the history
Add Output Video Option
  • Loading branch information
ergo720 authored Jan 31, 2024
2 parents 688b6a4 + 73fa966 commit cba5711
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 44 deletions.
77 changes: 47 additions & 30 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <hal/debug.h>
#include <pbkit/pbkit.h>
#include <hal/video.h>
#include <hal/xbox.h>
#include <hal/video.h> // for XVideoSetMode
#include <hal/xbox.h> // for XReboot
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -18,6 +17,15 @@
#define GIT_VERSION "unknown"
#endif

// defined in util/output.h file, used privately here only
extern BOOL output_video;
// Initialize the actual default values here if the config file is either successfully loaded before reading inputs or it failed to load.
static void init_default_values()
{
// NOTE: Pre-init is set to FALSE because video initialization doesn't occur until after the config file is loaded.
output_video = TRUE;
}

int load_conf_file(char *file_path)
{
print("Trying to open config file: %s", file_path);
Expand All @@ -30,46 +38,51 @@ int load_conf_file(char *file_path)
FILE_ATTRIBUTE_NORMAL,
NULL
);
if(handle == INVALID_HANDLE_VALUE) {
if (handle == INVALID_HANDLE_VALUE) {
print("Could not open config file '%s' for read", file_path);
return -1;
}

DWORD file_size = GetFileSize(handle, NULL);
if(file_size == INVALID_FILE_SIZE) {
if (file_size == INVALID_FILE_SIZE) {
print("ERROR: Could not get file size for %s", file_path);
return -1;
}

char* buffer = (char *)malloc(file_size);
if(buffer == NULL) {
if (buffer == NULL) {
print("Malloc failed for file_size %u", file_size);
return -1;
}

DWORD bytes_read = 0;
BOOL result = ReadFile(handle, buffer, file_size, &bytes_read, NULL);
if(result == 0 || bytes_read != file_size) {
if (result == 0 || bytes_read != file_size) {
print("Read failed for config file. result = %d, read = %u", file_size, bytes_read);
return -1;
}

CloseHandle(handle);
// Once the config file is loaded successfully, then initialize the actual default values (if any).
init_default_values();

char *line;
char *rest = buffer;
while ((line = strtok_r(rest, "\n", &rest))){
while ((line = strtok_r(rest, "\n", &rest))) {
char *current_key = strtok(line, "=");
if(strcmp("seed", current_key) == 0){
if (strcmp("seed", current_key) == 0) {
seed = strtoul(strtok(NULL, "\n"), NULL, 16);
}
if(strcmp("tests", current_key) == 0){
if (strcmp("tests", current_key) == 0) {
char *current_test;
char *tests = strtok(NULL, "\n");
while((current_test = strtok_r(tests, ",", &tests))) {
while ((current_test = strtok_r(tests, ",", &tests))) {
vector_append(&tests_to_run, strtol(current_test, NULL, 16));
}
}
if (strcmp("disable-video", current_key) == 0) {
output_video = !strtoul(strtok(NULL, "\n"), NULL, 16);
}
}

free(buffer);
Expand All @@ -79,50 +92,54 @@ int load_conf_file(char *file_path)
static void run_tests()
{
print("Random seed used is %u", seed);
if(tests_to_run.size == 0) {
if (tests_to_run.size == 0) {
print("No Specific tests specified. Running all tests (Single Pass).");
print("-------------------------------------------------------------");
int table_size = ARRAY_SIZE(kernel_thunk_table);
for(int k=0;k<table_size;k++){
for (int k=0; k<table_size; k++) {
kernel_thunk_table[k]();
}
}
else{
else {
print("Config File Was Loaded. Only running requested tests.");
print("-----------------------------------------------------");
for(int k=0; k<tests_to_run.size; k++){
for (int k=0; k<tests_to_run.size; k++) {
kernel_thunk_table[vector_get(&tests_to_run, k)]();
}
}
print("------------------------ End of Tests -----------------------");
}

void main(void){

XVideoSetMode(640, 480, 32, REFRESH_DEFAULT);
void main(void)
{
vector_init(&tests_to_run);
if (!open_output_file("D:\\kernel_tests.log")) {
return;
}
if (load_conf_file("D:\\config.txt")) {
init_default_values();
}

switch(pb_init()){
case 0: break;
default:
if (output_video) {
XVideoSetMode(640, 480, 32, REFRESH_DEFAULT);
// If the pb_init call returns non-zero, then something went wrong from nxdk's end.
if (pb_init()) {
Sleep(2000);
XReboot();
return;
}
pb_show_debug_screen();
}

pb_show_debug_screen();

vector_init(&tests_to_run);
open_output_file("D:\\kernel_tests.log");
load_conf_file("D:\\config.txt");

print("Kernel Test Suite");
print("build: " GIT_VERSION);
run_tests();


vector_free(&tests_to_run);
close_output_file();

Sleep(10000);
pb_kill();
if (output_video) {
Sleep(10000);
pb_kill();
}
}
26 changes: 14 additions & 12 deletions src/util/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "global.h"
#include "util/output.h"

BOOL output_video = FALSE; // NOTE: Must be set to a default of FALSE until config file is loaded and before video initialization.

static HANDLE output_filehandle = INVALID_HANDLE_VALUE;

void print(char* str, ...)
Expand All @@ -31,7 +33,9 @@ void print(char* str, ...)
strcat(buffer, "\n");

/** PRINT ON HARDWARE SCREEN ***/
debugPrint("%s", buffer);
if (output_video) {
debugPrint("%s", buffer);
}
/*******************************/

// Write information to logfile
Expand All @@ -58,9 +62,8 @@ void print_test_footer(
}
}

void open_output_file(char* file_path)
BOOL open_output_file(char* file_path)
{
debugPrint("Creating file %s\n", file_path);
output_filehandle = CreateFile(
file_path,
GENERIC_WRITE,
Expand All @@ -71,9 +74,7 @@ void open_output_file(char* file_path)
NULL
);

if(output_filehandle == INVALID_HANDLE_VALUE) {
debugPrint("ERROR: Could not create file %s\n", file_path);
}
return output_filehandle != INVALID_HANDLE_VALUE;
}

int write_to_output_file(
Expand All @@ -88,22 +89,23 @@ int write_to_output_file(
&bytes_written,
NULL
);
if(!ret) {
if(!ret && output_video) {
debugPrint("ERROR: Could not write to output file\n");
}
if(bytes_written != num_bytes_to_print) {
debugPrint("ERROR: Bytes written = %lu, bytes expected to write = %lu\n",
bytes_written, num_bytes_to_print);
if (output_video) {
debugPrint("ERROR: Bytes written = %lu, bytes expected to write = %lu\n",
bytes_written, num_bytes_to_print);
}
ret = 1;
}
return ret;
}

BOOL close_output_file()
void close_output_file()
{
BOOL ret = CloseHandle(output_filehandle);
if(!ret) {
debugPrint("ERROR: Could not close output file\n");
print("ERROR: Could not close output file");
}
return ret;
}
4 changes: 2 additions & 2 deletions src/util/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ void print_test_footer(const char*, const char*, BOOL);

// Real hardware can only display one screen of text at a time. Create an output
// logfile to contain information for all tests.
void open_output_file(char*);
BOOL open_output_file(char*);
int write_to_output_file(void*, DWORD);
BOOL close_output_file();
void close_output_file();

0 comments on commit cba5711

Please sign in to comment.