#include <SPI.h>
#include <SD.h>
#include <String.h>
#define DATA_OUT 23
#define DATA_CLK 25
#define LATCH_CLK 27
#define INSTR_OE 29
#define CLK 31
byte code_byte[16];
byte micro_code[2] = {0,0};
byte index_byte[3] = {0,0,0};
byte index_cmd = 0;
int index_int = 0;
String ScriptCommandLine = " ";
String ScriptCommandLineTest = " ";
//bool NotFirstCommandLine = false;
File Code;
File Script;
File Commands;
//fetch command characters. define location of mircocode.
void script(){
Script = SD.open("script.txt");
Serial.println("Opened textfile: 'script.txt'");
Serial.println("Script cursor position: ");
Serial.println(Script.position(), DEC);
Script.seek(index_cmd);
// Serial.print("index_cmd = ");
// Serial.println(index_cmd);
// Serial.println("Script cursor position: ");
// Serial.println(Script.position(), DEC);
ScriptCommandLine = Script.readStringUntil(48);
Serial.print("ScriptCommandLine: ");
Serial.print(ScriptCommandLine);
Serial.println("\n");
index_cmd += Script.position();
Script.close();
Serial.println("Closed textfile: 'script.txt'");
Commands = SD.open("commands.txt");
Serial.println("Opened textfile: 'commands.txt'");
Serial.print('\n');
while(!(ScriptCommandLineTest == ScriptCommandLine)){
Serial.println(Commands.position(), DEC);
ScriptCommandLineTest = " ";
ScriptCommandLineTest = Commands.readStringUntil(58);
//Serial.println(ScriptCommandLine);
Serial.println(Commands.position(), DEC);
Serial.println(ScriptCommandLineTest);
for(int i = 0; i < 3; i++){
index_byte[i] = Commands.read() - 48;
}
}
index_int = index_byte[0] * 100 + index_byte[1] * 10 + index_byte[2];
Serial.println(index_int);
Commands.close();
Serial.println("Closed textfile: 'commands.txt'");
//NotFirstCommandLine = true;
}
//fetch microcode. convert the microcode to usable form.
void microcode(){
Code = SD.open("code.txt");
Serial.println("Opened textfile: 'code.txt'");
Code.seek(index_int);
for (int i = 0; i < 16; i++){
code_byte[i] = Code.read() - 48;
}//Serial.print("code_byte: ");
for (int i = 0; i < 16; i++){
//Serial.print(code_byte[i], DEC);
}//Serial.println("\n");
Code.close();
Serial.println("Closed textfile: 'code.txt'");
for(int i = 0; i < 8; i++){
micro_code[0] += pow(2, 7 - i) * code_byte[i] + 0.0001;
// Serial.print("i = ");
// Serial.println(i, DEC);
// Serial.print("micro_code[0] = ");
// Serial.println(micro_code[0], DEC);
}//Serial.println("\n");
for(int i = 8; i < 16; i++){
micro_code[1] += pow(2, 15 - i) * code_byte[i] + 0.0001;
// Serial.print("i = ");
// Serial.println(i, DEC);
// Serial.print("micro_code[1] = ");
// Serial.println(micro_code[1], DEC);
}
}
//send data to shift registers
void mcc_out(){
shiftOut(DATA_OUT, DATA_CLK, LSBFIRST, micro_code[1]);
shiftOut(DATA_OUT, DATA_CLK, LSBFIRST, micro_code[0]);
digitalWrite(LATCH_CLK, HIGH);
delay(10);
digitalWrite(LATCH_CLK, LOW);
digitalWrite(INSTR_OE, LOW);
}
void setup() {
pinMode(INSTR_OE, OUTPUT);
pinMode(CLK, OUTPUT);
digitalWrite(INSTR_OE, HIGH);
digitalWrite(CLK, LOW);
pinMode(DATA_OUT, OUTPUT);
pinMode(DATA_CLK, OUTPUT);
pinMode(LATCH_CLK, OUTPUT);
digitalWrite(DATA_OUT, LOW);
digitalWrite(DATA_CLK, LOW);
digitalWrite(LATCH_CLK, LOW);
shiftOut(DATA_OUT, DATA_CLK, LSBFIRST, B01111111);
shiftOut(DATA_OUT, DATA_CLK, LSBFIRST, B01111111);
digitalWrite(LATCH_CLK, HIGH);
delay(10);
digitalWrite(LATCH_CLK, LOW);
digitalWrite(INSTR_OE, LOW);
delay(1000);
Serial.begin(9600);
while (!Serial) {; // wait for serial port to connect. Needed for native USB port only
}Serial.print("Initializing sd card...");
if (!SD.begin(53)) {
Serial.println("failed!");
while(1);
}Serial.println("initialization done.");
script();
//Script: Retrieve a line of command from "script.txt".
microcode();
//Code: Determine which command from "script.txt" by searching "commands.txt".
// Retrieve location of microcode from "commands.txt" and find microcode from "code.txt".
mcc_out();
//Output microcode from the command found "script.txt".
Serial.println(micro_code[0], BIN);
Serial.println(micro_code[1], BIN);
delay(5000);
script();
microcode();
mcc_out();
Serial.println(micro_code[0], BIN);
Serial.println(micro_code[1], BIN);
}
void loop() {
// put your main code here, to run repeatedly:
}