From 8648dd7b53a3577fb54857e277af05304f68474c Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 10 Jul 2021 15:30:23 +0200 Subject: Fixed PC load in interrupts --- lc3.c | 52 ++++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) (limited to 'lc3.c') diff --git a/lc3.c b/lc3.c index aea29b7..f21ce27 100644 --- a/lc3.c +++ b/lc3.c @@ -124,32 +124,6 @@ uint16_t sign_extend(uint16_t value, const int bits) return (value ^ mask) - mask; } -void fire(const uint16_t interrupt, const uint16_t priority) -{ - const uint16_t current_priority = (registers[REGISTER_PSR] >> 8) & 0x07; - if (0x00 != interrupt && 0x01 != interrupt && current_priority > priority) - { - return; - } - if (0x80 == interrupt && 0 == (0x4000 & memory[MEMORY_KBSR])) - { - return; - } - if (FLAG_SUP & registers[REGISTER_PSR]) - { - registers[REGISTER_PSR] &= ~FLAG_SUP; - registers[REGISTER_USR] = registers[REGISTER_R6]; - registers[REGISTER_R6] = registers[REGISTER_SSR]; - } - registers[REGISTER_R6]--; - memory[registers[REGISTER_R6]] = registers[REGISTER_PSR]; - registers[REGISTER_R6]--; - memory[registers[REGISTER_R6]] = registers[REGISTER_PC]; - registers[REGISTER_PC] = 0x0100 | (interrupt & 0x00ff); - registers[REGISTER_PSR] &= 0xfeff; - registers[REGISTER_PSR] |= priority << 8; -} - uint16_t check_key() { fd_set fds; @@ -187,6 +161,32 @@ uint16_t read_memory(const uint16_t address) return memory[address]; } +void fire(const uint16_t interrupt, const uint16_t priority) +{ + const uint16_t current_priority = (registers[REGISTER_PSR] >> 8) & 0x07; + if (0x00 != interrupt && 0x01 != interrupt && current_priority > priority) + { + return; + } + if (0x80 == interrupt && 0 == (0x4000 & memory[MEMORY_KBSR])) + { + return; + } + if (FLAG_SUP & registers[REGISTER_PSR]) + { + registers[REGISTER_PSR] &= ~FLAG_SUP; + registers[REGISTER_USR] = registers[REGISTER_R6]; + registers[REGISTER_R6] = registers[REGISTER_SSR]; + } + registers[REGISTER_R6]--; + memory[registers[REGISTER_R6]] = registers[REGISTER_PSR]; + registers[REGISTER_R6]--; + memory[registers[REGISTER_R6]] = registers[REGISTER_PC]; + registers[REGISTER_PC] = read_memory(0x0100 | (interrupt & 0x00ff)); + registers[REGISTER_PSR] &= 0xfeff; + registers[REGISTER_PSR] |= priority << 8; +} + void write_memory(const uint16_t address, const uint16_t value) { if (MEMORY_DDR == address) -- cgit v1.1