assembles the string to an instruction located at addr, storing opcodes in output buffer
| 12 | #include <cstdio> |
| 13 | ///assembles the string to an instruction located at addr, storing opcodes in output buffer |
| 14 | int Assemble(unsigned char *output, int addr, char *str) { |
| 15 | //unsigned char opcode[3] = { 0,0,0 }; |
| 16 | output[0] = output[1] = output[2] = 0; |
| 17 | char astr[128],ins[4]; |
| 18 | int len = strlen(str); |
| 19 | if ((!len) || (len > 127)) return 1; |
| 20 | |
| 21 | strcpy(astr,str); |
| 22 | str_ucase(astr); |
| 23 | sscanf(astr,"%3s",ins); //get instruction |
| 24 | if (strlen(ins) != 3) return 1; |
| 25 | strcpy(astr,strstr(astr,ins)+3); //heheh, this is probably a bad idea, but let's do it anyway! |
| 26 | if ((astr[0] != ' ') && (astr[0] != 0)) return 1; |
| 27 | |
| 28 | //remove all whitespace |
| 29 | str_strip(astr,STRIP_SP|STRIP_TAB|STRIP_CR|STRIP_LF); |
| 30 | |
| 31 | //repair syntax |
| 32 | chr_replace(astr,'[','('); //brackets |
| 33 | chr_replace(astr,']',')'); |
| 34 | chr_replace(astr,'{','('); |
| 35 | chr_replace(astr,'}',')'); |
| 36 | chr_replace(astr,';',0); //comments |
| 37 | str_replace(astr,"0X","$"); //miscellaneous |
| 38 | |
| 39 | //This does the following: |
| 40 | // 1) Sets opcode[0] on success, else returns 1. |
| 41 | // 2) Parses text in *astr to build the rest of the assembled |
| 42 | // data in 'opcode', else returns 1 on error. |
| 43 | |
| 44 | if (!strlen(astr)) { |
| 45 | //Implied instructions |
| 46 | if (!strcmp(ins,"BRK")) output[0] = 0x00; |
| 47 | else if (!strcmp(ins,"PHP")) output[0] = 0x08; |
| 48 | else if (!strcmp(ins,"ASL")) output[0] = 0x0A; |
| 49 | else if (!strcmp(ins,"CLC")) output[0] = 0x18; |
| 50 | else if (!strcmp(ins,"PLP")) output[0] = 0x28; |
| 51 | else if (!strcmp(ins,"ROL")) output[0] = 0x2A; |
| 52 | else if (!strcmp(ins,"SEC")) output[0] = 0x38; |
| 53 | else if (!strcmp(ins,"RTI")) output[0] = 0x40; |
| 54 | else if (!strcmp(ins,"PHA")) output[0] = 0x48; |
| 55 | else if (!strcmp(ins,"LSR")) output[0] = 0x4A; |
| 56 | else if (!strcmp(ins,"CLI")) output[0] = 0x58; |
| 57 | else if (!strcmp(ins,"RTS")) output[0] = 0x60; |
| 58 | else if (!strcmp(ins,"PLA")) output[0] = 0x68; |
| 59 | else if (!strcmp(ins,"ROR")) output[0] = 0x6A; |
| 60 | else if (!strcmp(ins,"SEI")) output[0] = 0x78; |
| 61 | else if (!strcmp(ins,"DEY")) output[0] = 0x88; |
| 62 | else if (!strcmp(ins,"TXA")) output[0] = 0x8A; |
| 63 | else if (!strcmp(ins,"TYA")) output[0] = 0x98; |
| 64 | else if (!strcmp(ins,"TXS")) output[0] = 0x9A; |
| 65 | else if (!strcmp(ins,"TAY")) output[0] = 0xA8; |
| 66 | else if (!strcmp(ins,"TAX")) output[0] = 0xAA; |
| 67 | else if (!strcmp(ins,"CLV")) output[0] = 0xB8; |
| 68 | else if (!strcmp(ins,"TSX")) output[0] = 0xBA; |
| 69 | else if (!strcmp(ins,"INY")) output[0] = 0xC8; |
| 70 | else if (!strcmp(ins,"DEX")) output[0] = 0xCA; |
| 71 | else if (!strcmp(ins,"CLD")) output[0] = 0xD8; |
no test coverage detected