Optimized main control loop
This commit is contained in:
parent
76942dc97e
commit
8cc1594b96
|
@ -3,21 +3,6 @@
|
|||
|
||||
#include "stm32f4xx_hal.h"
|
||||
|
||||
typedef enum {
|
||||
CONTROL_STOP = 0,
|
||||
CONTROL_RESET_BEGIN,
|
||||
CONTROL_RESET_1,
|
||||
CONTROL_RESET_2,
|
||||
CONTROL_RESET_3,
|
||||
CONTROL_RESET_4,
|
||||
CONTROL_RESET_5,
|
||||
CONTROL_RESET_6,
|
||||
CONTROL_RESET_7,
|
||||
CONTROL_RESET_END = 9,
|
||||
CONTROL_CLOCK_LOW,
|
||||
CONTROL_CLOCK_HIGH
|
||||
} ControlState;
|
||||
|
||||
typedef struct {
|
||||
uint8_t dirty;
|
||||
uint8_t action;
|
||||
|
@ -31,7 +16,6 @@ typedef struct {
|
|||
} Storage;
|
||||
|
||||
typedef struct {
|
||||
ControlState state;
|
||||
uint8_t memory_config;
|
||||
Storage storage;
|
||||
} Control;
|
||||
|
@ -41,7 +25,7 @@ void write_data(uint8_t value);
|
|||
uint8_t read_data();
|
||||
|
||||
void control_program_eeprom(uint8_t* data, uint16_t length);
|
||||
void control_execute_state();
|
||||
void control_cycle();
|
||||
void control_reset();
|
||||
|
||||
#endif
|
||||
|
|
|
@ -55,7 +55,7 @@ void control_program_eeprom(uint8_t* data, uint16_t length) {
|
|||
// Take control of the bus
|
||||
send_busrq(1);
|
||||
while (!has_busak()) {
|
||||
control_execute_state();
|
||||
control_cycle();
|
||||
}
|
||||
|
||||
enable_address_out(1);
|
||||
|
@ -237,7 +237,9 @@ void handle_ioreq() {
|
|||
}
|
||||
}
|
||||
|
||||
void cycle() {
|
||||
void control_cycle() {
|
||||
set_clock(1);
|
||||
|
||||
// We need this not detect IO multiple times
|
||||
static uint8_t had_ioreq = 0;
|
||||
if (!has_ioreq()) {
|
||||
|
@ -257,58 +259,20 @@ void cycle() {
|
|||
} else if (has_ioreq() && has_m1()) {
|
||||
printf("Interrupt ackknowledged\n\r");
|
||||
}
|
||||
}
|
||||
|
||||
void control_execute_state() {
|
||||
switch (control.state) {
|
||||
case CONTROL_STOP:
|
||||
// OK
|
||||
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET);
|
||||
// UPDATE
|
||||
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_4, GPIO_PIN_RESET);
|
||||
// OTHER
|
||||
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_5, GPIO_PIN_RESET);
|
||||
return;
|
||||
|
||||
case CONTROL_RESET_BEGIN:
|
||||
control.state++;
|
||||
set_reset(1);
|
||||
|
||||
// OK
|
||||
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_SET);
|
||||
// UPDATE
|
||||
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_4, GPIO_PIN_RESET);
|
||||
// OTHER
|
||||
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_5, GPIO_PIN_RESET);
|
||||
break;
|
||||
|
||||
case CONTROL_RESET_BEGIN+1 ... CONTROL_RESET_END-1:
|
||||
control.state++;
|
||||
set_clock(control.state % 2);
|
||||
break;
|
||||
|
||||
case CONTROL_RESET_END:
|
||||
control.state++;
|
||||
set_reset(0);
|
||||
break;
|
||||
|
||||
case CONTROL_CLOCK_LOW:
|
||||
control.state++;
|
||||
set_clock(1);
|
||||
break;
|
||||
|
||||
case CONTROL_CLOCK_HIGH:
|
||||
control.state--;
|
||||
cycle();
|
||||
set_clock(0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// @todo Properly reset everything
|
||||
void control_reset() {
|
||||
free(control.storage.buffer);
|
||||
Control temp = {CONTROL_RESET_BEGIN, 0, {1, 0, 0, 0, 0, 0, 0, 0, NULL}};
|
||||
Control temp = {0, {1, 0, 0, 0, 0, 0, 0, 0, NULL}};
|
||||
control = temp;
|
||||
control.storage.buffer = (uint8_t*)malloc(SD_PAGES*SD_PAGE_SIZE);
|
||||
|
||||
set_reset(1);
|
||||
for (int i = 0; i <= 10; ++i) {
|
||||
set_clock(i % 2);
|
||||
}
|
||||
set_reset(0);
|
||||
}
|
||||
|
|
|
@ -264,7 +264,7 @@ int main(void)
|
|||
printf("Restarting Z80\n\r");
|
||||
control_reset();
|
||||
} else if (!temp) {
|
||||
control_execute_state();
|
||||
control_cycle();
|
||||
}
|
||||
zrst = temp;
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user