prjbrook

spi0SlaveN.ino

Aug 23rd, 2020 (edited)
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.75 KB | None | 0 0
  1. // Written by Nick Gammon.
  2. // February 2011. PB: going to test three-pin PCINT0_vect
  3. //SS pin interrupt OK and receiving two adr Hi and Lo from Master with 2uS delay
  4. /* Fri Jul 3 11:02:43 NZST 2020. Thu Jul 9 15:07:04 NZST 2020 C:\Users\Dell\Documents\Arduino\PB_Sketches_May20\spi0SlaveA\spi0SlaveA.ino
  5. Fri Jul 10 11:08:13 NZST 2020. Not working well reading addresses.
  6. Sun Jul 12 12:50:23 NZST 2020. After 3 days work, back going ok. Fetch,execute problems and stray data. Given
  7. ..up on Pin Change with SS. At last, working OK. Just timing problems, I think.
  8. Mon Jul 13 11:50:24 NZST 2020 Now starting to add old parts of CDP1802f03.ino that worked on Arduino Mega
  9. Tue Jul 14 11:08:16 NZST 2020. Now trying check for commands etc.
  10. Wed Jul 15 11:56:17 NZST 2020 C:\Users\Dell\Documents\Arduino\PBSketchesjul2020\spi0SlaveF\spi0SlaveF.ino
  11. ..trying readstrobe stuff.Wed Jul 15 12:35:40 NZST 2020. Writing using writeStrobe seems to work.
  12. ..Now trying getFile so don't have to write hex bytes at top of this file.OK worked.
  13. Now going to try running in high adr =$4000. Forth inner interp goes OK in VRAM. Thu Jul 23 12:03:55 NZST 2020
  14.  
  15. Sat Jul 25 11:56:43 NZST 2020. All OK outputting hex, now want to do disassembly. INvestigate first: printLevel
  16. Added several important routines and flash table for disassem. May need some work to integrate CDP1802f03
  17. .. with two nano system. Wed Jul 29 12:36:21 NZST 2020. Going OK . Now insert single stepping.
  18. Fri Jul 31 12:32:31 NZST 2020. Now going to add breakpoint routines. Versioning got confused around K,L
  19. Sat Aug 1 12:44:58 NZST 2020: starting to integrate opcode stopping.
  20. Thu Aug 6 13:11:13 NZST 2020. Works OK. As do printLevels. with <de00> get fastest rate , about 26K
  21. ..for clock rate. Nearly finished. Do we need to turn commands on,off? Finally need to run a bigger prog.
  22. ..eg. forth1802A5.asm
  23. */
  24. #include <SPI.h>
  25. #include <avr/pgmspace.h> //not needed? Maybe will be needed with disassembly
  26. #define clockTo1802Pin 3
  27. #define SSpin 10
  28. #define clearPin 18//A4
  29. #define SCOPin 2
  30. #define MRDPin 8
  31. #define writePin 9
  32.  
  33.  
  34. const byte numChars = 32;
  35.  
  36. char receivedChars[numChars];
  37. char char3 = '7' ;
  38. char char4 ='F';
  39. char oldChar4;
  40. char data0[10] ;
  41. char closingBracket;
  42. char ch1;
  43. char instructionBytes = 1; //some instructions have 1 eg c4 = nop, some have 3 eg lsk 12 34
  44.  
  45. byte receivedBytes[numChars];
  46. volatile byte c;
  47. boolean newData = false;
  48. boolean showCommands=true;
  49. unsigned long t0,clkCtr=0,TPBCtr= 0, clockRate=0,t1=0; //used in millis timing.
  50. unsigned long time =0;
  51. unsigned int breakPointAdr=0,stopCode;
  52.  
  53.  
  54. volatile boolean SSFell2 = true;
  55.  
  56. uint16_t PB2Ints=0;
  57. uint16_t SSFallen=0;
  58. uint16_t DELAY = 32; //32; //100;
  59. uint16_t BUFFERLENGTH = 256 ; //1024; //this is how big the vRAM[] array is. Going to be shorter than 1024.
  60. uint16_t SPIintNUm=0;
  61. uint16_t ISRNum =0;
  62. uint16_t currentAdr=0;
  63. uint16_t opCode; //shoud be byte?
  64. uint16_t fetchCtr=0;
  65. int printLevel=3; //2; //how much detail do you want in serial output for disassembler
  66.  
  67. char fetchOrExecute ='N'; //neither at the start
  68. char readOrWriteCh;
  69. char data[100] ;
  70.  
  71. byte adr0 = 0; //this is high adr byte. adr1 is lo byte
  72. byte adr1 = 0;
  73. byte LSNib;
  74. byte MSNib;
  75. byte nextByte;
  76. byte thirdByte;
  77.  
  78. boolean gotAdr = false;
  79. boolean fetchFlag = false;
  80. boolean MRDFlag = false;
  81. boolean writeStrobe = false;
  82. boolean foundOpCode;
  83. boolean singleStepping = false, stepMe=false;
  84. boolean doOpStops = false; //breakpoint if opCode=stopCode;
  85.  
  86. const char flashHiNib3[] PROGMEM = {"[00idl.][01ldn.L[10inc.L[20dec.L[30br.N[31bq.N[32bz.N[33bdf.N[34b1.N[35b2.N[36b3.N[37b4.N[38skp.][39bnq.N[3Abnz.N[3Bbnf.N[3Cbn1.N[3Dbn2.N[3Ebn3.N[3Fbn4.N[40lda.L[50str.L [60irx.L[61out.L[69inp.L[70ret.]abcd[71dis.][72ldxa.][73stxd][74adc.][75sdb.][76shrc.][77smb.][78sav.][79mark.][7Aseq.][7Breq.][7Caddi.N[7Dsdbi.N[7Eshlc.][7Fsmbi.N[80glo.L[90ghi.L[A0plo.L[B0phi.L[C0lbr.T[C1lbq.T[C2lbz.T[C3lbdf.T[C4nop.][C5lsnq.][C6lsnz.][C7lsnf.][C8lskp.][C9lbnq.T[CAlbnz.T[CBlbnf.T[CClsie.][CDlsq.][CElsz.][CFlsdf.][D0sep.L[E0sex.L[F0ldx.][F1or.][F2and.][F3xor.][F4add.][F5sd.][F6shr.][F7sm.][F8ldi.N[F9ori.N[FAani.N[FBxri.N[FCadi.N[FDsdi.N[FEshl.][FFsmi.N"}; //};
  87.  
  88. //byte vRAM[5] = {0x7b, 0x7b, 0x7a, 0x7b, 0x00}; //seq is 7b (wrong in RCA book)
  89. //byte vRAM[20] = {0x7b,0x7a,0x7a,0x7a,0x7a,0xc4,0xc4,0x7b,0x7a,0x30,0x05,0x00}; //seq is 7b, led on with req. (wrong in RCA book)
  90. //byte vRAM[256] = {0x7b,0x7a,0x7a,0x7a,0x7a,0xc4,0xc4,0x7b,0x7a,0x30,0x05,0x00};
  91. byte vRAM[256] = {0x7b,0x7a,0xf8,0,0xb2,0xf8,0xf8,0xa2,0xf8,0xa6,0x52,0x7a,0xc4,0xc4,0x7b,0x7a,0x30,0x0c,0x00};
  92. //byte vRAM[256] = {0xc4, 0xc0, 0x40, 0x00, 0x7b,0x7a,0x7a,0x7a,0x7a,0xc4,0xc4,0x7b,0x7a,0x30,0x05,0x00}; // just jump to highadr = $4000
  93.  
  94. void setup (void) {
  95. pinMode(clockTo1802Pin,OUTPUT); //D3 goes to 1802 clock
  96. pinMode(SSpin,INPUT);
  97. pinMode(clearPin,OUTPUT);
  98. pinMode(SCOPin,INPUT);
  99. pinMode(MRDPin,INPUT);
  100.  
  101. Serial.begin (115200); // debugging
  102. serialFlush();
  103. delay(500); //time to sort out Bray++
  104. Serial.flush();
  105. doReset();
  106.  
  107. SPCR =(1<<SPE); // turn on SPI in slave mode
  108. pinMode(MISO, OUTPUT); // have to send output on master in, *slave out*
  109. SPI.attachInterrupt();
  110. SPDR=0; //to start off with
  111. dumpVariableVert(256); //**
  112. } // end of setup
  113.  
  114.  
  115.  
  116. // SPI interrupt routine
  117. ISR (SPI_STC_vect) { //only want to come here when SS pin is low
  118. c = SPDR; // grab byte from SPI Data Register
  119. if (SSFell2==true){
  120. adr0=c;
  121. SSFell2=false;
  122. } else {
  123. adr1=c; //adr1 is low adr
  124. gotAdr=true;
  125. SSFell2=true;
  126. } //need 2uS gap between adresses to make this work.
  127. SPIintNUm++ ;
  128. //next, quickly get status flags before too late.
  129. if (digitalRead(SCOPin) == LOW ) fetchFlag = true; else fetchFlag=false;
  130. if (digitalRead(MRDPin) == LOW) MRDFlag = true; else MRDFlag = false;
  131. // end of interrupt routine SPI_STC_vect
  132. }
  133. //-----------------------------
  134. void loop (void) {
  135. checkForCommands();
  136. doClockCycle();
  137. } //---------------------
  138.  
  139. void doClockCycle(void) {
  140. digitalWrite(clockTo1802Pin,HIGH);
  141. if (digitalRead(writePin)==LOW){
  142. writeStrobe=true;
  143. //doInpStuffIfNecess();
  144. }
  145. delay(DELAY);
  146.  
  147. digitalWrite(clockTo1802Pin,LOW);
  148. if ( gotAdr==true) {
  149. doGotAdr();
  150. TPBCtr++; //Serial.print(TPBCtr);Serial.print(" ");Serial.println(clkCtr);
  151. }
  152. clkCtr++;
  153. delay(DELAY);
  154. }
  155. void doGotAdr(void) {
  156.  
  157. if (MRDFlag==true) {
  158. currentAdr = adr1+adr0*256;
  159. //Serial.print("~~~currentAdr");Serial.println(currentAdr,HEX);
  160. if (adr0 ==0) { //if low adr it's in vRAM array, otherwise lives in high flash
  161. opCode = vRAM[currentAdr];
  162. sendToBus(vRAM[adr1]);
  163. nextByte = vRAM[currentAdr+1];
  164. thirdByte = vRAM[currentAdr+2];
  165. }
  166. else {
  167. opCode = pgm_read_byte(currentAdr);
  168. sendToBus(pgm_read_byte(currentAdr)); //must have been a hihg byte in flash $4000+
  169. nextByte = pgm_read_byte(currentAdr+1);
  170. thirdByte = pgm_read_byte(currentAdr+2);
  171. }
  172. if(printLevel!=0) {
  173. printTPBResults5();
  174. }
  175. if(singleStepping==true){
  176. Serial.print("*");
  177. stepMe=false;
  178. while(stepMe==false){
  179. checkForCommands();
  180. }
  181. }
  182. if((currentAdr==breakPointAdr) && (breakPointAdr>0)) {
  183. Serial.println("Breakpoint address .....");
  184. singleStepping=true;
  185. }
  186. if((doOpStops==true) && (opCode == stopCode)){
  187. if (fetchFlag==true) { //works better with this extra fetchFlag condition
  188. Serial.println("Op code stop .....");
  189. singleStepping=true;
  190. }
  191. }
  192. }
  193. if (writeStrobe==true) {
  194. writeStrobe = false;
  195. vRAM[adr1] = getFRomBus();
  196. }
  197.  
  198. gotAdr=false;
  199. }
  200.  
  201.  
  202. void sendToBus(byte b) { //works OK
  203. byte temp;
  204. DDRC = DDRC | 0x0f; //low nibble on PortC
  205. DDRD = DDRD | 0xf0;
  206. PORTC = (PORTC & 0xf0) | (b & 0x0f);
  207. PORTD = (PORTD & 0x0f) | ( b & 0xf0);
  208. if(fetchFlag==true) {
  209. fetchOrExecute ='F';
  210. fetchCtr++;
  211.  
  212. //Serial.print(currentAdr,HEX);Serial.print(" is adr, and current opCode is ");Serial.print(opCode,HEX); Serial.print(" <-Fetch ");
  213. }
  214. else {
  215. //Serial.print(" execute ");
  216. fetchOrExecute='E';
  217. //Serial.print(fetchOrExecute);
  218. }
  219. if(MRDFlag==true) {
  220. //Serial.println(" Read4 ");
  221. readOrWriteCh = 'R';
  222. }
  223. else {
  224. Serial.println(" NoRead "); //@@@@@@@@@@@@
  225. readOrWriteCh = 'W';
  226. }
  227.  
  228. }
  229. byte getFRomBus(void) { //used when !WRB goes low
  230. byte lob,hib,inpb;
  231. DDRC = DDRC & 0xf0; //low nibble on PortC
  232. DDRD = DDRD & 0x0f; //Maybe put these DDR commands up into calling fn to slow it down, before read.
  233. //Serial.print(DDRC,HEX);Serial.print(" <--DDRC and DDRD --> ");Serial.println (DDRD);
  234. //delay(1); //needed, PINC and PIND might need settling time
  235. delayMicroseconds(4); //needs this delay in order to read port properly after DDR change.
  236. lob= PINC & 0x0f;
  237. hib = PIND & 0xf0;
  238. inpb = (hib | lob );
  239. if (printLevel==3) {
  240. Serial.print ("-w-"); Serial.print(adr0,HEX);
  241. Serial.print(adr1,HEX);Serial.print(" is adr, and going into vRAM is ");Serial.println(inpb,HEX);
  242. }
  243. return inpb;
  244.  
  245. }
  246.  
  247. void doReset(void){
  248. Serial.println("Resetting---");
  249. digitalWrite(clearPin,LOW); //bring CLEAR low for about 10 clocks to reset 1802
  250. for(int i =0;i<=10;i++) {
  251. digitalWrite(clockTo1802Pin, HIGH);
  252. delay(DELAY);
  253. digitalWrite(clockTo1802Pin, LOW);
  254. delay(DELAY);
  255. }// 10 clocks low should reset 1802
  256. digitalWrite(clearPin,HIGH); //bring back to RUN mode
  257. }
  258.  
  259. void serialFlush(void){
  260. while(Serial.available() > 0) { //while there are characters in the serial buffer, because
  261. char t = Serial.read(); // get one character
  262. }
  263. }
  264. void dumpVariableVert(int sizeToDump) { //From old mega based version
  265. Serial.print("Col: ");
  266. for(int i=0;i<16;i++) {
  267. sprintf(data, "%02x ",i);
  268. Serial.print(data);
  269. if(i==7) Serial.print(" ");
  270. }
  271. for (int i=0;i<sizeToDump;i++){
  272. if (i%16 == 0){
  273. Serial.println ("");
  274. sprintf(data, "%02x ",i);
  275. Serial.print(data);
  276. }
  277. if(i%8==0) { Serial.print("|"); }
  278. sprintf(data, " %02x ",vRAM[i]);
  279. Serial.print(data);
  280. }
  281. Serial.println(" ");
  282. }
  283. //printTPBResults4 seems to be best output routine from old mega code
  284. void checkForCommands(){ //if any inputs from keyboard, push them into buffer and see what command it is.
  285. recvWithStartEndMarkers();
  286. showAndInterpret();
  287. }
  288. void recvWithStartEndMarkers() { ////https://forum.arduino.cc/index.php?topic=396450
  289. static boolean recvInProgress = false;
  290. static byte ndx = 0;
  291. char startMarker = '<';
  292. char endMarker = '>';
  293. char rc;
  294. while (Serial.available() > 0 && newData == false) {
  295. rc = Serial.read();
  296.  
  297. if (recvInProgress == true) {
  298. if (rc != endMarker) {
  299. receivedChars[ndx] = rc;
  300. receivedBytes[ndx]=(byte)rc; //PB added this
  301. ndx++;
  302. if (ndx >= numChars) {
  303. ndx = numChars - 1;
  304. }
  305. }
  306. else {
  307. receivedChars[ndx] = '\0'; // terminate the string
  308. recvInProgress = false;
  309. ndx = 0;
  310. newData = true;
  311. }
  312. }
  313.  
  314. else if (rc == startMarker) {
  315. recvInProgress = true;
  316. }
  317. }
  318. }
  319. void showAndInterpret() {
  320. if (newData == true) {
  321. if(showCommands==true){
  322. Serial.print("Command received is : ");
  323. Serial.println(receivedChars);
  324. }
  325. else {serialFlush();} //get rid of commands in input buffer
  326. interpretCommand();
  327. //interpretCommand(); //*******put back** just testing for ram size
  328. newData = false;
  329. }
  330. }
  331. void interpretCommand() {
  332. // String Command(receivedChars); //**important
  333. //if(Command.equals("gf")){
  334. if((receivedBytes[0]=='g')&&(receivedBytes[1]=='f')) { //get file
  335. Serial.print("Send file");
  336. getFile();
  337. Serial.print(" Not working yet in two nano system.");
  338. serialFlush();
  339. doReset();
  340. }
  341. if((receivedBytes[0]=='d')&&(receivedBytes[1]=='u')) { //dump 256 bytes;"du"
  342. dumpVariableVert(256);
  343. serialFlush();
  344. }
  345. if((receivedBytes[0]=='r')&&(receivedBytes[1]=='e')) { //"re" for reset
  346. doReset();
  347. TPBCtr=0; clkCtr = 0;
  348. serialFlush();
  349. Serial.println(F("**done gf flush** Now resetting"));
  350. }
  351. if((receivedBytes[0]=='s')&&(receivedBytes[1]=='s')){ //single step; "ss"
  352. singleStepping=true; t1=millis()-time; Serial.print("--> ");Serial.print(t1);
  353. clockRate = (clkCtr*1000)/t1; Serial.print("=&& "); Serial.println(clockRate);
  354. } //go into single stepping but stop clock; presume <run> just finished.
  355.  
  356. if((receivedBytes[0]=='s')&&(receivedBytes[1]=='t')) stepMe=true; //"st"
  357.  
  358. if((receivedBytes[0]=='r')&&(receivedBytes[1]=='u')) { //"ru"; run
  359. singleStepping=false;
  360. stepMe=true;
  361. Serial.println("Sing step = false");
  362. clkCtr=0; TPBCtr=0; fetchCtr=0; //after "run" we're timing clks/sec
  363. time=millis();
  364. }
  365. if((receivedBytes[0]==0x64)&&(receivedBytes[1]==0x65)) doDelay(); //<dexx>.
  366. if((receivedBytes[0]==0x62)&&(receivedBytes[1]==0x70)) doBreakPoint2();
  367. if((receivedBytes[0]==0x6f)&&(receivedBytes[1]==0x70)) doOpStop2(); //<opxx>
  368. if((receivedBytes[0]=='o')&&(receivedBytes[1]=='s')) { //os ; opstop toggle
  369. if(doOpStops==true) doOpStops=false;
  370. else doOpStops=true;
  371. }
  372. if((receivedBytes[0]=='r')&&(receivedBytes[1]=='p')) doReport2(); //doReport(); //rp
  373. if((receivedBytes[0]=='p')&&(receivedBytes[1]=='0')) printLevel=0; //p0
  374. if((receivedBytes[0]=='p')&&(receivedBytes[1]=='1')) printLevel=1; //p1
  375. if((receivedBytes[0]=='p')&&(receivedBytes[1]=='2')) printLevel=2; //p2
  376. if((receivedBytes[0]=='p')&&(receivedBytes[1]=='3')) printLevel=3; //p3
  377. }
  378. void getFile() { // go to File Send part of Bray++ terminal and put bin file into vRAM[]
  379. int inCtr=0;
  380. byte byteIn=65;
  381. while(inCtr<BUFFERLENGTH){
  382. if(Serial.available()){
  383. byteIn = Serial.read();
  384. vRAM[inCtr]=byteIn;
  385. inCtr++;
  386. }
  387. }
  388. Serial.print("Got "); Serial.println(BUFFERLENGTH);
  389. }
  390. void doDelay() {
  391. unsigned int hexVal[8];
  392. hexVal[2]=hexCharToInt2(receivedBytes[2])*16;
  393. hexVal[3]=hexCharToInt2(receivedBytes[3])*1;
  394. DELAY=hexVal[2]+hexVal[3];
  395. Serial.print("DELAY now is : ");
  396. Serial.print(DELAY);
  397. Serial.print(", in hex : 0x");
  398. Serial.println(DELAY,HEX);
  399. }
  400. void printTPBResults5(void) { //this fn taken mostly from CDP1802f03.asm
  401. Serial.print("[");
  402. if (fetchOrExecute=='F'){
  403. sprintf(data," %02x%02x : ",adr0,adr1);
  404. Serial.print(data);
  405. //Serial.print(fetchOrExecute);
  406. //doDisAsm(opCode); //old .
  407. doDisLine(opCode); //BIG connection with new progmem code
  408. if (printLevel==1) { //@@@@@@@@@keep print level at 1 for time being
  409. Serial.println("]"); //if level1 finish here with CR..
  410. }
  411. if(printLevel>=2) {
  412. sprintf(data," --%2x ",opCode);
  413. Serial.print(data);
  414. if(closingBracket==']') {Serial.println(); }
  415. if(closingBracket=='L') {Serial.println();} //Serial.println(LSNib,HEX);
  416. if(closingBracket=='N') Serial.println(nextByte,HEX); //eg br 34.
  417. if(closingBracket=='T') {
  418. //Serial.print(nextByte,HEX);
  419. //Serial.println(thirdByte,HEX);
  420. //Serial.print(closingBracket);
  421. sprintf(data," %02x%02x ",nextByte,thirdByte);
  422. Serial.print(data);
  423.  
  424. } /*
  425. //if (instructionBytes ==1) Serial.println(data);
  426. if (instructionBytes==2) {
  427. //Serial.print(data);
  428. //Serial.println(vRAM[currentAdr+1],HEX); //nextByte = vRAM[currentAdr+1];
  429. //Serial.println(nextByte,HEX);
  430. }
  431. if (instructionBytes==3) {
  432. //Serial.print(data);
  433. //Serial.print(vRAM[currentAdr+1],HEX);
  434. //Serial.println(nextByte,HEX);
  435. //Serial.println(vRAM[currentAdr+2],HEX); //thirdByte = vRAM[currentAdr+2];
  436. //Serial.println(thirdByte,HEX);
  437. } */
  438. }
  439. }
  440. else { // ie it's not 'F', it's an execute line:
  441. if(printLevel==3) { //*************pit back
  442. sprintf(data," Adr: %x%x : %02x Ck=%lu, Cy=%lu, Fc=%u ; ( %c %c ) M%lu Cr%lu",adr0,adr1,opCode,clkCtr,TPBCtr,fetchCtr, fetchOrExecute,readOrWriteCh,t1,clockRate);
  443. // sprintf(data," Adr: %x%x : %02x Ck=%lu, Cy=%lu, In=%lu ; ( %c %c ) M%lu Cr%lu",adr0,adr0,opCode,clkCtr,TPBCtr,fetchCtr, fetchOrExecute,readOrWriteCh,t1,clockRate);
  444. //sprintf(data,"Adr: %x%x : %02x %u, %u ,%u ; %c %c",adrByteHi,adrByte,opCode,TPBCtr,clkCtr,fetchCtr, fetchOrExecuteCh,readOrWriteCh);
  445. //sprintf(data," Adr: %x%x : %02x %u, %u %c %c",adrByteHi,adrByte,opCode,TPBCtr,clkCtr, fetchOrExecuteCh,readOrWriteCh);
  446. Serial.println(data);
  447. //Serial.println(fetchOrExecute);
  448. }
  449. }
  450. }
  451. //next routines come from CDP1802f03.asm
  452. void doDisLine( byte opC) { //go into big progrmem string and emrge with j pointing opening '[' of menonic substring
  453. opCodePreProcess(opC);
  454. int k=0;
  455. foundOpCode= false;
  456. while ((k < strlen_P(flashHiNib3)) && (foundOpCode==false)){
  457. ch1 = pgm_read_byte_near(flashHiNib3+k);
  458. if (ch1=='[') {
  459. if( (pgm_read_byte_near(flashHiNib3+k+1)==char3) && (pgm_read_byte_near(flashHiNib3+k+2) ==char4) ){
  460. foundOpCode=true;
  461. data0[0] = pgm_read_byte_near(flashHiNib3+k+3); //first char of mnemonic
  462. data0[1] = pgm_read_byte_near(flashHiNib3+k+4);
  463. data0[2] = pgm_read_byte_near(flashHiNib3+k+5);
  464. data0[3] = pgm_read_byte_near(flashHiNib3+k+6);
  465. data0[4] = pgm_read_byte_near(flashHiNib3+k+7);
  466. data0[5] = pgm_read_byte_near(flashHiNib3+k+8);
  467. if (data0[2] == '.') closingBracket = data0[3]; //two char menonic like br
  468. if (data0[3] == '.') closingBracket = data0[4]; //3 char mnem like lda.
  469. if (data0[4] == '.') closingBracket = data0[5];
  470. }
  471. }
  472. k++;
  473. }
  474. if (foundOpCode==false) {
  475. Serial.print(F("Don't understandop code "));
  476. Serial.println(opC,HEX);
  477. }
  478. else {
  479. fixData0(); // now the nemonic ends with trailing \0
  480. Serial.print (data0); //print the mnemonic..
  481. doClosingBrackets(); //..and any other addresses etc
  482. }
  483. }
  484. //
  485. void opCodePreProcess(byte opCo0) {
  486. MSNib = opCo0/16;
  487. LSNib = opCo0%16;
  488. char3 = binToAsc(MSNib);
  489. char4= binToAsc(LSNib);
  490. if ((char3 =='8')||(char3 =='9') || (char3 =='A')|| (char3 =='B')) { oldChar4 = char4; char4='0'; } //glo,ghi,plo,phi resp
  491. if (char3=='0') { if (LSNib>0) {oldChar4 = char4; char4='1';}} //ldn but n!=0
  492. if ((char3=='1') || (char3=='2')) {oldChar4 = char4; char4='0';} //inc, dec
  493. if ((char3=='4') || (char3=='5')) {oldChar4 = char4; char4='0';} //lda,str
  494. if (char3=='6') {
  495. if ((LSNib>0) && (LSNib<=7)){oldChar4 = char4; char4='1';} //out n but n!=0 and n<=7
  496. if (LSNib>8) {oldChar4 = char4; char4='9';LSNib=LSNib-8;} //inp n but n>8. NB 68 seems to be invalid opCode
  497. }
  498. if ((char3=='D') || (char3=='E')) {oldChar4 = char4; char4='0';} //sep,sex
  499. }
  500. byte binToAsc(byte b) { //eg change 0x03 to 0x33 = '3'
  501. byte temp;
  502. temp = b + 0x30;
  503. if (temp > 0x39) temp = temp +7; // adjust for hex. NB outputs capital A,B etc
  504. return temp;
  505. }
  506. //
  507. void fixData0(void) { //replace dot with space and \0 so it prints OK
  508. for (int i = 0;i<10;i++) {
  509. if (data0[i] == '.') {
  510. data0[i] = ' ';
  511. data0[i+1] ='\0';
  512. }
  513. }
  514. }
  515. void doClosingBrackets(void) { //going to change printlns to prints below. To work in with old software display..
  516. if(closingBracket==']') { }; // Serial.print("."); //nothing after mnemonic eg nop
  517. if(closingBracket=='L') Serial.print(LSNib,HEX); //eg phi 3
  518. if(closingBracket=='N') Serial.print(nextByte,HEX); //eg br 34.
  519. if(closingBracket=='T') {
  520. Serial.print(nextByte,HEX);
  521. Serial.print(thirdByte,HEX);
  522. }
  523. }
  524. int hexCharToInt2(byte by){ //pb version
  525. int d,r;
  526. d=by-'0';
  527. if(d>=0 && d<=9) {r=d;}
  528. else{
  529. d=by-55; //is it A,B...F?
  530. if (d>=10 && d<=15) {r=d;}
  531. else{
  532. r=by-87; //must be a,b...f. NB No error checking.
  533. }
  534. }
  535. return r;
  536. }
  537. void doBreakPoint2(){
  538. int d;
  539. int ctr=0;
  540. unsigned int hexVal[8];
  541. hexVal[2]=hexCharToInt2(receivedBytes[2])*4096;
  542. hexVal[3]=hexCharToInt2(receivedBytes[3])*256;
  543. hexVal[4]=hexCharToInt2(receivedBytes[4])*16;
  544. hexVal[5]=hexCharToInt2(receivedBytes[5])*1;
  545. breakPointAdr=hexVal[2]+hexVal[3]+hexVal[4]+hexVal[5]; //important
  546. Serial.print("Break point adress is : ");
  547. Serial.print(breakPointAdr);
  548. Serial.print(", in hex : 0x");
  549. Serial.println(breakPointAdr,HEX);
  550. }
  551. void doOpStop2(){
  552. // int d;
  553. unsigned int hexVal[8];
  554. hexVal[2]=hexCharToInt2(receivedBytes[2])*16;
  555. hexVal[3]=hexCharToInt2(receivedBytes[3])*1;
  556. stopCode=hexVal[2]+hexVal[3];
  557. Serial.print("Op stop is : ");
  558. Serial.print(stopCode);
  559. Serial.print(", in hex : 0x");
  560. Serial.println(stopCode,HEX);
  561. }
  562. void doReport2() { //old doReport takes up too much RAM
  563. Serial.println (F("delay,op-Stop code, breakpoint"));
  564. Serial.println(DELAY);
  565. Serial.println(stopCode,HEX);
  566. Serial.println(breakPointAdr);
  567. }
  568.  
Add Comment
Please, Sign In to add comment