Skip to content

Commit

Permalink
#12 Save and load BASIC programs
Browse files Browse the repository at this point in the history
  • Loading branch information
hagronnestad committed Sep 19, 2019
1 parent 472dd7b commit 5dfea38
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 19 deletions.
3 changes: 3 additions & 0 deletions ComputerSystems/Commodore64/C64MemoryLocations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@ public static class C64MemoryLocations {
/// Write: Raster line to generate interrupt at (bits #0-#7)
/// </summary>
public const ushort CURRENT_RASTER_LINE = 0xD012;

public const ushort POINTER_TO_BEGINNING_OF_BASIC_AREA_LO = 0x002B;
public const ushort POINTER_TO_BEGINNING_OF_BASIC_AREA_HI = 0x002C;
}
}
3 changes: 3 additions & 0 deletions ComputerSystems/Commodore64/C64MemoryOffsets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ public static class C64MemoryOffsets {
/// 0x0277 - 0x0280
/// </summary>
public const ushort KEYBOARD_BUFFER = 0x0277;

public const ushort DEFAULT_BASIC_AREA_START = 0x0801;
public const ushort DEFAULT_BASIC_AREA_END = 0x9FFF;
}
}
74 changes: 55 additions & 19 deletions ComputerSystems/Commodore64/FormC64Screen.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions ComputerSystems/Commodore64/FormC64Screen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
using System.Windows.Forms;
using Extensions.Byte;
using Extensions.Enums;
using System.IO;
using System.Linq;
using System.Collections.Generic;

namespace ComputerSystem.Commodore64 {
public partial class FormC64Screen : Form {
Expand Down Expand Up @@ -185,5 +188,42 @@ private void BtnCopyScreenBuffer_Click(object sender, EventArgs e) {
private void BtnCopyScaledScreenBuffer_Click(object sender, EventArgs e) {
Clipboard.SetImage(_bC64ScreenOutputBuffer);
}

private void BtnOpen_Click(object sender, EventArgs e) {
if (ofd.ShowDialog() == DialogResult.OK) {

var file = File.ReadAllBytes(ofd.FileName);

var address = BitConverter.ToUInt16(file, 0);
var data = file.Skip(2).ToArray();

for (int i = 0; i < data.Length; i++) {
C64.Memory[address + i] = data[i];
}

}
}

private void BtnSave_Click(object sender, EventArgs e) {
if (sfd.ShowDialog() == DialogResult.OK) {

var basicAreaLength = C64MemoryOffsets.DEFAULT_BASIC_AREA_END - C64MemoryOffsets.DEFAULT_BASIC_AREA_START;
var data = new List<byte>();

data.AddRange(BitConverter.GetBytes((ushort)0x0801));

for (int i = 0; i < basicAreaLength; i++) {
data.Add(C64.Memory[C64MemoryOffsets.DEFAULT_BASIC_AREA_START + i]);

// TODO: Fix this horrible check
// The BASIC program ends with 3 NULL-bytes, so break when we find them
if (data.Count >= 3 && data.Skip(data.Count - 3).Take(3).All(x => x == 0x00)) {
break;
}
}

File.WriteAllBytes(sfd.FileName, data.ToArray());
}
}
}
}
6 changes: 6 additions & 0 deletions ComputerSystems/Commodore64/FormC64Screen.resx
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,10 @@
<metadata name="toolMain.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>232, 17</value>
</metadata>
<metadata name="ofd.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>333, 17</value>
</metadata>
<metadata name="sfd.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>403, 17</value>
</metadata>
</root>

0 comments on commit 5dfea38

Please sign in to comment.