From 0895ce02b2bc603c76818d84203ffce960cef7a5 Mon Sep 17 00:00:00 2001 From: Sam Stevens Date: Mon, 12 Nov 2018 21:36:22 +0000 Subject: [PATCH] Changed ADD to clear and populate carry. Run/Step only when not halted. Reversed speed control. --- arduino/Interface.cpp | 12 +++++------- arduino/ShiftInput.cpp | 37 ++++++++++++++++++++++++++++--------- arduino/VM.c | 8 +++----- arduino/VM.h | 2 +- arduino/arduino.ino | 12 ++++++++---- 5 files changed, 45 insertions(+), 26 deletions(-) diff --git a/arduino/Interface.cpp b/arduino/Interface.cpp index 7a86eb3..2ab80fc 100644 --- a/arduino/Interface.cpp +++ b/arduino/Interface.cpp @@ -55,17 +55,19 @@ void Interface::loop() { uint8_t addrInputL = ~reverse8(input->getInput(1)); uint8_t swInput = input->getInput(2); uint16_t addrInput = (addrInputH << 8) + addrInputL; + //Check for buttons on rising edge if ((swLast & SW_RUN) == 0 && (swInput & SW_RUN) == SW_RUN) { if (vm->run) { vm->run = false; } else { - vm->run = true; - vm->halted = false; + if (vm->halted == false) { + vm->run = true; + } } } else if ((swLast & SW_STEP) == 0 && (swInput & SW_STEP) == SW_STEP) { - if (vm->run == false) { + if (vm->run == false && vm->halted == false) { vm->halted = false; vm_step(vm); } @@ -148,10 +150,6 @@ void Interface::loop() { //Program counter and Instruction uint16_t pc = vm->PC; uint16_t instr = (mem->read(pc) << 8) + mem->read(pc + 1); -// disp->output((uint8_t)0xF); -// disp->output((uint8_t)0xF); -// disp->output((uint8_t)0xF); -// disp->output((uint8_t)0xF); disp->output16(instr); disp->output16(pc); diff --git a/arduino/ShiftInput.cpp b/arduino/ShiftInput.cpp index 415b91a..0b0a3be 100644 --- a/arduino/ShiftInput.cpp +++ b/arduino/ShiftInput.cpp @@ -20,10 +20,12 @@ limitations under the License. ShiftInput::ShiftInput(uint8_t chipCount, uint8_t loadPin, uint8_t clockPin, uint8_t clockInhPin, uint8_t dataPin) { this->input = (uint8_t*)calloc(chipCount, sizeof(uint8_t)); this->chipCount = chipCount; + this->loadPin = loadPin; this->clockPin = clockPin; this->clockInhPin = clockInhPin; this->dataPin = dataPin; + pinMode(loadPin, OUTPUT); digitalWrite(loadPin, HIGH); pinMode(clockPin, OUTPUT); @@ -47,23 +49,39 @@ uint8_t ShiftInput::getChipCount() { } bool ShiftInput::updateInput() { + uint8_t dataBit = digitalPinToBitMask(this->dataPin); + volatile uint8_t *dataPort = portInputRegister(digitalPinToPort(this->dataPin)); + uint8_t loadBit = digitalPinToBitMask(this->loadPin); + volatile uint8_t *loadPort = portOutputRegister(digitalPinToPort(this->loadPin)); + uint8_t clkBit = digitalPinToBitMask(this->clockPin); + volatile uint8_t *clkPort = portOutputRegister(digitalPinToPort(this->clockPin)); + uint8_t clkInhBit = 0; + volatile uint8_t *clkInhPort = NULL; + if (this->clockInhPin > 0){ + clkInhBit = digitalPinToBitMask(this->clockInhPin); + clkInhPort = portOutputRegister(digitalPinToPort(this->clockInhPin)); + } + uint8_t oldSREG = SREG; + cli(); + //Load values - digitalWrite(this->loadPin, LOW); - digitalWrite(this->clockPin, HIGH); - digitalWrite(this->loadPin, HIGH); - digitalWrite(this->clockPin, LOW); + *loadPort &= ~loadBit; //Load Low + *clkPort |= clkBit; //Clock High + *loadPort |= loadBit; //Load High + *clkPort &= ~clkBit; //Clock Low bool change = false; uint8_t input = 0; if (this->clockInhPin > 0){ - digitalWrite(this->clockInhPin, LOW); + *clkInhPort &= ~clkInhBit;//Clock Inh Low } + for(uint8_t chip = 0; chip < this->chipCount; chip++) { input = 0; for(uint8_t inputBit = 0; inputBit < 8; inputBit++) { - input |= (digitalRead(this->dataPin) == HIGH ? 1 : 0) << inputBit; - digitalWrite(clockPin, HIGH); - digitalWrite(clockPin, LOW); + *clkPort &= ~clkBit; //Clock Low + input |= ((*dataPort & dataBit) ? 1 : 0) << inputBit; + *clkPort |= clkBit; //Clock High } if (this->input[chip] != input) { this->input[chip] = input; @@ -71,8 +89,9 @@ bool ShiftInput::updateInput() { } } if (this->clockInhPin > 0){ - digitalWrite(this->clockInhPin, HIGH); + *clkInhPort |= clkInhBit; //Clock Inh High } + SREG = oldSREG; return change; } diff --git a/arduino/VM.c b/arduino/VM.c index 6365f15..7a8f98a 100644 --- a/arduino/VM.c +++ b/arduino/VM.c @@ -71,6 +71,9 @@ inline uint16_t vm_get_rx(VM *vm, uint8_t r) { } inline void vm_put_r(VM *vm, uint8_t r, uint8_t v) { + if (r == 0) { + return; + } if (r < VM_REG_SIZE) { vm->R[r] = v; } @@ -164,12 +167,7 @@ void vm_step(VM *vm) { rd = inst.imm; break; case OP_ADD: - vm_decode_S(&inst, raw); vm->carry = 0; - rx = vm_get_r(vm, inst.rx); - ry = vm_get_r(vm, inst.ry); - rd = rx + ry; - break; case OP_ADC: vm_decode_S(&inst, raw); rx = vm_get_r(vm, inst.rx); diff --git a/arduino/VM.h b/arduino/VM.h index 5677576..97032df 100644 --- a/arduino/VM.h +++ b/arduino/VM.h @@ -42,7 +42,7 @@ limitations under the License. #define VM_ERR_MISALIGN 0x1 #define VM_ERR_UNKNOWN_OP 0x2 -#define VM_ERR_OUT_OF_BOUNDS 0x3 +#define VM_ERR_OUT_OF_BOUNDS 0x4 #ifdef __cplusplus extern "C" { diff --git a/arduino/arduino.ino b/arduino/arduino.ino index a25ca87..82aafb1 100644 --- a/arduino/arduino.ino +++ b/arduino/arduino.ino @@ -16,7 +16,8 @@ limitations under the License. /** * Example Programs - * Hello World: 311833021210c0323401411412103416e4203406d400000048656c6c6f20576f726c640a + * Hello World: 3118 3302 1210 c032 3401 4114 1210 3416 e420 3406 d400 0000 48656c6c6f20576f726c640a + * 32-bit Counter: 3601 3704 4556 5440 5330 5220 5110 D700 */ //Options @@ -111,12 +112,13 @@ void setup() { } unsigned long int nextRun = 0; +uint8_t err = 0; void loop() { if (vm.run) { if (nextRun == 0 || nextRun < millis()) { vm_step(&vm); - int delayTime = analogRead(PIN_SPEED); + int delayTime = 1024 - analogRead(PIN_SPEED); if (delayTime < 10) { nextRun = 0; } else { @@ -132,10 +134,12 @@ void loop() { } void vm_print_error(uint8_t err) { - Serial.print("\n"); + vm.R[0] = err; + Serial.print("\nPC 0x"); + Serial.print(vm.PC, HEX); switch(err) { case VM_ERR_MISALIGN: - Serial.println("Halted. PC misaligned."); + Serial.println("\nHalted. PC misaligned."); break; case VM_ERR_UNKNOWN_OP: //Will only happen if an instruction is not handled in the vm