Changed ADD to clear and populate carry.

Run/Step only when not halted.
Reversed speed control.
This commit is contained in:
2018-11-12 21:36:22 +00:00
parent 7aa4aa79fb
commit 0895ce02b2
5 changed files with 45 additions and 26 deletions

View File

@@ -55,17 +55,19 @@ void Interface::loop() {
uint8_t addrInputL = ~reverse8(input->getInput(1)); uint8_t addrInputL = ~reverse8(input->getInput(1));
uint8_t swInput = input->getInput(2); uint8_t swInput = input->getInput(2);
uint16_t addrInput = (addrInputH << 8) + addrInputL; uint16_t addrInput = (addrInputH << 8) + addrInputL;
//Check for buttons on rising edge //Check for buttons on rising edge
if ((swLast & SW_RUN) == 0 && (swInput & SW_RUN) == SW_RUN) { if ((swLast & SW_RUN) == 0 && (swInput & SW_RUN) == SW_RUN) {
if (vm->run) { if (vm->run) {
vm->run = false; vm->run = false;
} else { } else {
if (vm->halted == false) {
vm->run = true; vm->run = true;
vm->halted = false; }
} }
} }
else if ((swLast & SW_STEP) == 0 && (swInput & SW_STEP) == SW_STEP) { 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->halted = false;
vm_step(vm); vm_step(vm);
} }
@@ -148,10 +150,6 @@ void Interface::loop() {
//Program counter and Instruction //Program counter and Instruction
uint16_t pc = vm->PC; uint16_t pc = vm->PC;
uint16_t instr = (mem->read(pc) << 8) + mem->read(pc + 1); 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(instr);
disp->output16(pc); disp->output16(pc);

View File

@@ -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) { 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->input = (uint8_t*)calloc(chipCount, sizeof(uint8_t));
this->chipCount = chipCount; this->chipCount = chipCount;
this->loadPin = loadPin; this->loadPin = loadPin;
this->clockPin = clockPin; this->clockPin = clockPin;
this->clockInhPin = clockInhPin; this->clockInhPin = clockInhPin;
this->dataPin = dataPin; this->dataPin = dataPin;
pinMode(loadPin, OUTPUT); pinMode(loadPin, OUTPUT);
digitalWrite(loadPin, HIGH); digitalWrite(loadPin, HIGH);
pinMode(clockPin, OUTPUT); pinMode(clockPin, OUTPUT);
@@ -47,23 +49,39 @@ uint8_t ShiftInput::getChipCount() {
} }
bool ShiftInput::updateInput() { 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 //Load values
digitalWrite(this->loadPin, LOW); *loadPort &= ~loadBit; //Load Low
digitalWrite(this->clockPin, HIGH); *clkPort |= clkBit; //Clock High
digitalWrite(this->loadPin, HIGH); *loadPort |= loadBit; //Load High
digitalWrite(this->clockPin, LOW); *clkPort &= ~clkBit; //Clock Low
bool change = false; bool change = false;
uint8_t input = 0; uint8_t input = 0;
if (this->clockInhPin > 0){ if (this->clockInhPin > 0){
digitalWrite(this->clockInhPin, LOW); *clkInhPort &= ~clkInhBit;//Clock Inh Low
} }
for(uint8_t chip = 0; chip < this->chipCount; chip++) { for(uint8_t chip = 0; chip < this->chipCount; chip++) {
input = 0; input = 0;
for(uint8_t inputBit = 0; inputBit < 8; inputBit++) { for(uint8_t inputBit = 0; inputBit < 8; inputBit++) {
input |= (digitalRead(this->dataPin) == HIGH ? 1 : 0) << inputBit; *clkPort &= ~clkBit; //Clock Low
digitalWrite(clockPin, HIGH); input |= ((*dataPort & dataBit) ? 1 : 0) << inputBit;
digitalWrite(clockPin, LOW); *clkPort |= clkBit; //Clock High
} }
if (this->input[chip] != input) { if (this->input[chip] != input) {
this->input[chip] = input; this->input[chip] = input;
@@ -71,8 +89,9 @@ bool ShiftInput::updateInput() {
} }
} }
if (this->clockInhPin > 0){ if (this->clockInhPin > 0){
digitalWrite(this->clockInhPin, HIGH); *clkInhPort |= clkInhBit; //Clock Inh High
} }
SREG = oldSREG;
return change; return change;
} }

View File

@@ -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) { inline void vm_put_r(VM *vm, uint8_t r, uint8_t v) {
if (r == 0) {
return;
}
if (r < VM_REG_SIZE) { if (r < VM_REG_SIZE) {
vm->R[r] = v; vm->R[r] = v;
} }
@@ -164,12 +167,7 @@ void vm_step(VM *vm) {
rd = inst.imm; rd = inst.imm;
break; break;
case OP_ADD: case OP_ADD:
vm_decode_S(&inst, raw);
vm->carry = 0; vm->carry = 0;
rx = vm_get_r(vm, inst.rx);
ry = vm_get_r(vm, inst.ry);
rd = rx + ry;
break;
case OP_ADC: case OP_ADC:
vm_decode_S(&inst, raw); vm_decode_S(&inst, raw);
rx = vm_get_r(vm, inst.rx); rx = vm_get_r(vm, inst.rx);

View File

@@ -42,7 +42,7 @@ limitations under the License.
#define VM_ERR_MISALIGN 0x1 #define VM_ERR_MISALIGN 0x1
#define VM_ERR_UNKNOWN_OP 0x2 #define VM_ERR_UNKNOWN_OP 0x2
#define VM_ERR_OUT_OF_BOUNDS 0x3 #define VM_ERR_OUT_OF_BOUNDS 0x4
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {

View File

@@ -16,7 +16,8 @@ limitations under the License.
/** /**
* Example Programs * 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 //Options
@@ -111,12 +112,13 @@ void setup() {
} }
unsigned long int nextRun = 0; unsigned long int nextRun = 0;
uint8_t err = 0;
void loop() { void loop() {
if (vm.run) { if (vm.run) {
if (nextRun == 0 || nextRun < millis()) { if (nextRun == 0 || nextRun < millis()) {
vm_step(&vm); vm_step(&vm);
int delayTime = analogRead(PIN_SPEED); int delayTime = 1024 - analogRead(PIN_SPEED);
if (delayTime < 10) { if (delayTime < 10) {
nextRun = 0; nextRun = 0;
} else { } else {
@@ -132,10 +134,12 @@ void loop() {
} }
void vm_print_error(uint8_t err) { 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) { switch(err) {
case VM_ERR_MISALIGN: case VM_ERR_MISALIGN:
Serial.println("Halted. PC misaligned."); Serial.println("\nHalted. PC misaligned.");
break; break;
case VM_ERR_UNKNOWN_OP: case VM_ERR_UNKNOWN_OP:
//Will only happen if an instruction is not handled in the vm //Will only happen if an instruction is not handled in the vm