From 8f9642b3513a9f3c0d8cf9cbeda0541dfabb2eed Mon Sep 17 00:00:00 2001
From: "adrian.spycher" <adrian.spycher@etu.hesge.ch>
Date: Tue, 5 Nov 2024 18:08:10 +0100
Subject: [PATCH] chore: fix state machine value and redo some printf

---
 vmm/operation.c | 24 ++++++++++++------------
 vmm/operation.h |  6 +++---
 2 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/vmm/operation.c b/vmm/operation.c
index 9a7f6b1..d68c7c0 100644
--- a/vmm/operation.c
+++ b/vmm/operation.c
@@ -31,16 +31,16 @@ state_t STATE_GFX_INIT[] = {
 
 state_t STATE_IDE[] = {
 
-    { OP_READ_INJECT,      REG_IDE_ST,       0,                1, op_callback_ide_prepare },
+    { OP_READ_INJECT,      REG_IDE_ST,       0x40,             1, op_callback_ide_prepare },
     { OP_WRITE_EQUAL,      REG_IDE_DATA + 2, 1,                1, NULL },
     { OP_WRITE_STORE,      REG_IDE_DATA + 3, 0,                1, op_callback_ide_store_sector_1 },
     { OP_WRITE_STORE,      REG_IDE_DATA + 4, 0,                1, op_callback_ide_store_sector_2 },
     { OP_WRITE_STORE,      REG_IDE_DATA + 5, 0,                1, op_callback_ide_store_sector_3 },
     { OP_WRITE_STORE,      REG_IDE_DATA + 6, 0,                1, op_callback_ide_store_sector_4 },
     { OP_WRITE_EQUAL,      REG_IDE_CMD,      0x30,             1, NULL },
-    { OP_READ_INJECT,      REG_IDE_ST,       0,                1, NULL },
+    { OP_READ_INJECT,      REG_IDE_ST,       0x40,             1, NULL },
     { OP_WRITE_STORE_LOOP, REG_IDE_DATA,     0,                2, op_callback_ide_store_data },
-    { OP_EMUL_END,         0,                0,                0, op_callback_ide_condlude },
+    { OP_EMUL_END,         0,                0,                0, op_callback_ide_conclude },
 };
 
 state_t *STATE_ALL_STARTERS[] = {
@@ -57,7 +57,7 @@ static hyper_init_gfx_params_t param_gfx_init;
 static hyper_ide_params_t param_ide;
 
 // store the index of the actual loop
-static uint32_t loop_ide_data;
+static uint32_t loop_ide_data = MAX_LOOP_IDE_DATA;
 static uint16_t buf_ide_data[MAX_LOOP_IDE_DATA];
 
 // --- STATIC FUNCTION ---
@@ -70,7 +70,7 @@ void op_callback_console_conclude(void *addr) {
 
     hyper_virtual_console_params_t *p_consol = (hyper_virtual_console_params_t *)addr;
 
-    printf("action : sending a message...\n");
+    printf("OPERATION : sending a message...\n");
     printf("%s\n", (char *)p_consol->msg);
 }
 
@@ -83,7 +83,7 @@ void op_callback_timer_conclude(void *addr) {
 
     hyper_timer_sleep_params_t *p_timer = (addr == NULL) ? &param_timer : (hyper_timer_sleep_params_t *)addr;
 
-    printf("action : setting up a %dus timer...\n", p_timer->us);
+    printf("OPERATION : setting up a %dus timer...\n", p_timer->us);
     usleep(p_timer->us);
 }
 
@@ -101,7 +101,7 @@ void op_callback_gfx_init_conclude(void *addr) {
 
     hyper_init_gfx_params_t *p_gfx_init = (addr == NULL) ? &param_gfx_init : (hyper_init_gfx_params_t *)addr;
 
-    printf("action : initializing gfx %dx%d...\n", p_gfx_init->width, p_gfx_init->height);
+    printf("OPERATION : initializing gfx %dx%d...\n", p_gfx_init->width, p_gfx_init->height);
     width_gfx = p_gfx_init->width;
     height_gfx = p_gfx_init->height;
     sem_post(&sem_gfx);
@@ -133,7 +133,7 @@ void op_callback_ide_store_sector_3(void *addr) {
 void op_callback_ide_store_sector_4(void *addr) {
 
     param_ide.sector_idx |= ((*(uint8_t *)addr) & 0x0F) << 24;
-    // TODO: what to do with the mode ?
+    // WARNING: what to do with the mode ?
 }
 
 void op_callback_ide_store_data(void *addr) {
@@ -148,10 +148,10 @@ void op_callback_ide_store_data(void *addr) {
     }
 }
 
-void op_callback_ide_condlude(void *addr) {
+void op_callback_ide_conclude(void *addr) {
 
     hyper_ide_params_t *p_ide = (addr == NULL) ? &param_ide : (hyper_ide_params_t *)addr;
-    printf("action : writing on 0x%lx on sector %d\n", p_ide->data, p_ide->sector_idx);
+    printf("OPERATION : writing data on sector %d\n", p_ide->sector_idx);
 
     // check sector range
     if (p_ide->sector_idx > MAX_SECTOR_NUM) err(1, "IDE sector is out of range");
@@ -164,15 +164,15 @@ void op_callback_ide_condlude(void *addr) {
 
     // Seek to the sector position
     if (fseek(disk, offset, SEEK_SET) != 0) {
-        err(1, "IDE seek failed");
         fclose(disk);
+        err(1, "IDE seek failed");
     }
 
     // Write 512 bytes to the specified sector
     size_t bytes_written = fwrite((void *)p_ide->data, 1, SECTOR_SIZE, disk);
     if (bytes_written != SECTOR_SIZE) {
-        err(1, "IDE failed to write full sector");
         fclose(disk);
+        err(1, "IDE failed to write full sector");
     }
 
     fclose(disk);
diff --git a/vmm/operation.h b/vmm/operation.h
index ff9af81..f37e0ac 100644
--- a/vmm/operation.h
+++ b/vmm/operation.h
@@ -10,7 +10,7 @@
 
 // --- DEFINE ---
 
-#define STATE_MACHINE_NUM 2
+#define STATE_MACHINE_NUM 3
 
 typedef enum {
 
@@ -24,7 +24,7 @@ typedef enum {
 typedef struct {
 
     operation_t op;                 // the operation to perform
-    uint32_t addr;                  // the address written to/read from
+    uint64_t addr;                  // the address written to/read from
     uint32_t value;                 // the expected written value or value to inject
     uint8_t size;                   // the size of the operation (8, 16 or 32 bits)
     void (*callback)(void *addr);   // custom function that would be executed at the end of the state
@@ -102,6 +102,6 @@ void op_callback_ide_store_sector_4(void *addr);
 
 void op_callback_ide_store_data(void *addr);
 
-void op_callback_ide_condlude(void *addr);
+void op_callback_ide_conclude(void *addr);
 
 #endif // _OPERATION_H_
-- 
GitLab