private Boolean ConvertTextToBASIC(String[] textBuffer) { Boolean bREM = false; Boolean bDATA = false; Boolean bString = false; UInt16 ui16CurrentAddress = program.StartAddress; int iProgramIdx = 0; int iBufferIdx = 0; int iAddressIdx = 0; // Initialise the program buffer program.m_programData = new Byte[0xFFFF]; program.m_programData.Initialize(); for (int iLoop = 0; iLoop < textBuffer.Length; iLoop++) { if (textBuffer[iLoop].Length > 0) { bREM = false; bDATA = false; bString = false; // Find first character iBufferIdx = 0; iBufferIdx = FindNextNonSpaceChar(textBuffer[iLoop], iBufferIdx); String lineno = ""; // Get line no. while (char.IsNumber(GetNextChar(textBuffer[iLoop], iBufferIdx))) { lineno += textBuffer[iLoop].Substring(iBufferIdx, 1); iBufferIdx++; } UInt16 ui16LineNo = Convert.ToUInt16(lineno, 10); if (ui16LineNo < 0 || ui16LineNo > 0xFFFF) { MessageBox.Show("Invalid line no. specified.", "Line No. Invalid", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } iAddressIdx = iProgramIdx; program.m_programData[iProgramIdx] = 0xFF; iProgramIdx++; program.m_programData[iProgramIdx] = 0xFE; iProgramIdx++; program.m_programData[iProgramIdx] = (Byte)(ui16LineNo & 0xFF); iProgramIdx++; program.m_programData[iProgramIdx] = (Byte)(ui16LineNo >> 8); iProgramIdx++; iBufferIdx = FindNextNonSpaceChar(textBuffer[iLoop], iBufferIdx); Boolean bEndOfLine = false; while (!bEndOfLine) { if (bREM) { program.m_programData[iProgramIdx] = Convert.ToByte(textBuffer[iLoop][iBufferIdx]); iBufferIdx++; } else if (bString) { if (textBuffer[iLoop][iBufferIdx] == '"') { bString = false; } program.m_programData[iProgramIdx] = Convert.ToByte(textBuffer[iLoop][iBufferIdx]); iBufferIdx++; } else if (bDATA) { if (textBuffer[iLoop][iBufferIdx] == ':') { bDATA = false; } program.m_programData[iProgramIdx] = Convert.ToByte(textBuffer[iLoop][iBufferIdx]); iBufferIdx++; } else { int iToken = MatchKeyword(textBuffer[iLoop], iBufferIdx); if (iToken == 0x9D) // || textBuffer[iLoop][iBufferIdx] == '\'') { bREM = true; } if (iToken == 0x91) { bDATA = true; } if (textBuffer[iLoop][iBufferIdx] == '"') { bString = true; } if (iToken == 0) { program.m_programData[iProgramIdx] = Convert.ToByte(textBuffer[iLoop][iBufferIdx]); iBufferIdx++; } else { program.m_programData[iProgramIdx] = Convert.ToByte(iToken); String token = basicTokens.GetBasicToken((Byte)(iToken - 0x80)); iBufferIdx += token.Length; } } ui16CurrentAddress++; iProgramIdx++; if (iBufferIdx >= textBuffer[iLoop].Length) { ui16CurrentAddress += 5; program.m_programData[iAddressIdx] = (Byte)(ui16CurrentAddress & 0xFF); iAddressIdx++; program.m_programData[iAddressIdx] = (Byte)(ui16CurrentAddress >> 8); program.m_programData[iProgramIdx] = 0x00; iProgramIdx++; bEndOfLine = true; } } } } program.m_programData[iProgramIdx] = 0x00; iProgramIdx++; program.m_programData[iProgramIdx] = 0x00; program.EndAddress = (ushort)(program.StartAddress + iProgramIdx); FileStream fsm = new FileStream(textBoxDestinationFile.Text, FileMode.Create, FileAccess.Write); using (BinaryWriter binWriter = new BinaryWriter(fsm)) { // Write the Tape leader binWriter.Write((Byte)0x16); binWriter.Write((Byte)0x16); binWriter.Write((Byte)0x16); binWriter.Write((Byte)0x24); binWriter.Write((Byte)0x00); binWriter.Write((Byte)0x00); // Write the Program format if (program.Format == OricProgram.ProgramFormat.BasicProgram) { binWriter.Write((Byte)0x00); } else { binWriter.Write((Byte)0x01); } // Write the Auto run flag if (program.AutoRun == OricProgram.AutoRunFlag.Enabled) { binWriter.Write((Byte)0x00); } else { binWriter.Write((Byte)0x01); } program.EndAddress = (ushort)(program.StartAddress + iProgramIdx); // Get the High and Low bytes of the End address and write to file Byte hi = Convert.ToByte((program.EndAddress >> 8) & 0xFF); Byte lo = Convert.ToByte(program.EndAddress & 0xFF); binWriter.Write((Byte)hi); binWriter.Write((Byte)lo); // Get the High and Low bytes of the Start address and write to file hi = Convert.ToByte((program.StartAddress >> 8) & 0xFF); lo = Convert.ToByte(program.StartAddress & 0xFF); binWriter.Write((Byte)hi); binWriter.Write((Byte)lo); // Write unused byte binWriter.Write((Byte)0x00); int iIndex = 0; // Write the Program name for (iIndex = 0; iIndex < program.ProgramName.Length; iIndex++) { binWriter.Write(Convert.ToByte(program.ProgramName[iIndex])); } // NULL terminate the Program name binWriter.Write((Byte)0x00); // Write file data for (iIndex = 0; iIndex < iProgramIdx; iIndex++) { binWriter.Write((Byte)program.m_programData[iIndex]); } } fsm.Close(); return(true); }
private bool ConvertTextToBASIC(string[] textBuffer, ref OricProgram program) { try { bool bREM = false; bool bDATA = false; bool bString = false; ushort ui16CurrentAddress = program.StartAddress; int iProgramIdx = 0; int iBufferIdx = 0; int iAddressIdx = 0; // Initialise the program buffer program.ProgramData = new byte[0xFFFF]; program.ProgramData.Initialize(); for (int iLoop = 0; iLoop < textBuffer.Length; iLoop++) { if (textBuffer[iLoop].Length > 0) { bREM = false; bDATA = false; bString = false; // Find first character iBufferIdx = 0; iBufferIdx = FindNextNonSpaceChar(textBuffer[iLoop], iBufferIdx); string lineno = ""; // Get line no. while (char.IsNumber(GetNextChar(textBuffer[iLoop], iBufferIdx))) { lineno += textBuffer[iLoop].Substring(iBufferIdx, 1); iBufferIdx++; } ushort ui16LineNo = Convert.ToUInt16(lineno, 10); if (ui16LineNo < 0 || ui16LineNo > 0xFFFF) { MessageBox.Show("Invalid line no. specified.", "Import Atmos BASIC File", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } iAddressIdx = iProgramIdx; program.ProgramData[iProgramIdx] = 0xFF; iProgramIdx++; program.ProgramData[iProgramIdx] = 0xFE; iProgramIdx++; program.ProgramData[iProgramIdx] = (byte)(ui16LineNo & 0xFF); iProgramIdx++; program.ProgramData[iProgramIdx] = (byte)(ui16LineNo >> 8); iProgramIdx++; iBufferIdx = FindNextNonSpaceChar(textBuffer[iLoop], iBufferIdx); bool bEndOfLine = false; while (!bEndOfLine) { if (bREM) { program.ProgramData[iProgramIdx] = Convert.ToByte(textBuffer[iLoop][iBufferIdx]); iBufferIdx++; } else if (bString) { if (textBuffer[iLoop][iBufferIdx] == '"') { bString = false; } program.ProgramData[iProgramIdx] = Convert.ToByte(textBuffer[iLoop][iBufferIdx]); iBufferIdx++; } else if (bDATA) { if (textBuffer[iLoop][iBufferIdx] == ':') { bDATA = false; } program.ProgramData[iProgramIdx] = Convert.ToByte(textBuffer[iLoop][iBufferIdx]); iBufferIdx++; } else { int iToken = MatchKeyword(textBuffer[iLoop], iBufferIdx); if (iToken == 0x9D) // || textBuffer[iLoop][iBufferIdx] == '\'') { bREM = true; } if (iToken == 0x91) { bDATA = true; } if (textBuffer[iLoop][iBufferIdx] == '"') { bString = true; } if (iToken == 0) { program.ProgramData[iProgramIdx] = Convert.ToByte(textBuffer[iLoop][iBufferIdx]); iBufferIdx++; } else { program.ProgramData[iProgramIdx] = Convert.ToByte(iToken); string token = basicTokens.GetBasicToken((byte)(iToken - 0x80)); iBufferIdx += token.Length; } } ui16CurrentAddress++; iProgramIdx++; if (iBufferIdx >= textBuffer[iLoop].Length) { ui16CurrentAddress += 5; program.ProgramData[iAddressIdx] = (byte)(ui16CurrentAddress & 0xFF); iAddressIdx++; program.ProgramData[iAddressIdx] = (byte)(ui16CurrentAddress >> 8); program.ProgramData[iProgramIdx] = 0x00; iProgramIdx++; bEndOfLine = true; } } } } program.ProgramData[iProgramIdx] = 0x00; iProgramIdx++; program.ProgramData[iProgramIdx] = 0x00; program.EndAddress = (ushort)(program.StartAddress + iProgramIdx); return(true); } catch (Exception) { return(false); } }