diff --git a/ComputerSystems/Commodore64/C64MemoryLocations.cs b/ComputerSystems/Commodore64/C64MemoryLocations.cs index 510e8fa..71e6144 100644 --- a/ComputerSystems/Commodore64/C64MemoryLocations.cs +++ b/ComputerSystems/Commodore64/C64MemoryLocations.cs @@ -33,5 +33,8 @@ public static class C64MemoryLocations { /// Write: Raster line to generate interrupt at (bits #0-#7) /// 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; } } diff --git a/ComputerSystems/Commodore64/C64MemoryOffsets.cs b/ComputerSystems/Commodore64/C64MemoryOffsets.cs index fdf7b10..1022d89 100644 --- a/ComputerSystems/Commodore64/C64MemoryOffsets.cs +++ b/ComputerSystems/Commodore64/C64MemoryOffsets.cs @@ -17,5 +17,8 @@ public static class C64MemoryOffsets { /// 0x0277 - 0x0280 /// public const ushort KEYBOARD_BUFFER = 0x0277; + + public const ushort DEFAULT_BASIC_AREA_START = 0x0801; + public const ushort DEFAULT_BASIC_AREA_END = 0x9FFF; } } diff --git a/ComputerSystems/Commodore64/FormC64Screen.Designer.cs b/ComputerSystems/Commodore64/FormC64Screen.Designer.cs index 925478f..065a691 100644 --- a/ComputerSystems/Commodore64/FormC64Screen.Designer.cs +++ b/ComputerSystems/Commodore64/FormC64Screen.Designer.cs @@ -27,10 +27,14 @@ private void InitializeComponent() { this.statusMain = new System.Windows.Forms.StatusStrip(); this.lblFps = new System.Windows.Forms.ToolStripStatusLabel(); this.toolMain = new System.Windows.Forms.ToolStrip(); + this.btnRestart = new System.Windows.Forms.ToolStripButton(); + this.btnUseCrtFilter = new System.Windows.Forms.ToolStripButton(); this.btnCopyScreenBuffer = new System.Windows.Forms.ToolStripButton(); this.btnCopyScaledScreenBuffer = new System.Windows.Forms.ToolStripButton(); - this.btnUseCrtFilter = new System.Windows.Forms.ToolStripButton(); - this.btnRestart = new System.Windows.Forms.ToolStripButton(); + this.btnOpen = new System.Windows.Forms.ToolStripButton(); + this.ofd = new System.Windows.Forms.OpenFileDialog(); + this.btnSave = new System.Windows.Forms.ToolStripButton(); + this.sfd = new System.Windows.Forms.SaveFileDialog(); ((System.ComponentModel.ISupportInitialize)(this.pScreen)).BeginInit(); this.statusMain.SuspendLayout(); this.toolMain.SuspendLayout(); @@ -72,13 +76,35 @@ private void InitializeComponent() { this.btnRestart, this.btnUseCrtFilter, this.btnCopyScreenBuffer, - this.btnCopyScaledScreenBuffer}); + this.btnCopyScaledScreenBuffer, + this.btnOpen, + this.btnSave}); this.toolMain.Location = new System.Drawing.Point(0, 0); this.toolMain.Name = "toolMain"; this.toolMain.Size = new System.Drawing.Size(640, 25); this.toolMain.TabIndex = 0; this.toolMain.Text = "toolStrip1"; // + // btnRestart + // + this.btnRestart.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text; + this.btnRestart.ImageTransparentColor = System.Drawing.Color.Magenta; + this.btnRestart.Name = "btnRestart"; + this.btnRestart.Size = new System.Drawing.Size(47, 22); + this.btnRestart.Text = "Restart"; + this.btnRestart.Click += new System.EventHandler(this.BtnRestart_Click); + // + // btnUseCrtFilter + // + this.btnUseCrtFilter.Checked = true; + this.btnUseCrtFilter.CheckOnClick = true; + this.btnUseCrtFilter.CheckState = System.Windows.Forms.CheckState.Checked; + this.btnUseCrtFilter.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text; + this.btnUseCrtFilter.ImageTransparentColor = System.Drawing.Color.Magenta; + this.btnUseCrtFilter.Name = "btnUseCrtFilter"; + this.btnUseCrtFilter.Size = new System.Drawing.Size(58, 22); + this.btnUseCrtFilter.Text = "CRT filter"; + // // btnCopyScreenBuffer // this.btnCopyScreenBuffer.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text; @@ -97,25 +123,31 @@ private void InitializeComponent() { this.btnCopyScaledScreenBuffer.Text = "Copy output"; this.btnCopyScaledScreenBuffer.Click += new System.EventHandler(this.BtnCopyScaledScreenBuffer_Click); // - // btnUseCrtFilter + // btnOpen // - this.btnUseCrtFilter.Checked = true; - this.btnUseCrtFilter.CheckOnClick = true; - this.btnUseCrtFilter.CheckState = System.Windows.Forms.CheckState.Checked; - this.btnUseCrtFilter.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text; - this.btnUseCrtFilter.ImageTransparentColor = System.Drawing.Color.Magenta; - this.btnUseCrtFilter.Name = "btnUseCrtFilter"; - this.btnUseCrtFilter.Size = new System.Drawing.Size(58, 22); - this.btnUseCrtFilter.Text = "CRT filter"; + this.btnOpen.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text; + this.btnOpen.ImageTransparentColor = System.Drawing.Color.Magenta; + this.btnOpen.Name = "btnOpen"; + this.btnOpen.Size = new System.Drawing.Size(40, 22); + this.btnOpen.Text = "Open"; + this.btnOpen.Click += new System.EventHandler(this.BtnOpen_Click); // - // btnRestart + // ofd // - this.btnRestart.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text; - this.btnRestart.ImageTransparentColor = System.Drawing.Color.Magenta; - this.btnRestart.Name = "btnRestart"; - this.btnRestart.Size = new System.Drawing.Size(47, 22); - this.btnRestart.Text = "Restart"; - this.btnRestart.Click += new System.EventHandler(this.BtnRestart_Click); + this.ofd.Filter = "PRG-files|*.prg"; + // + // btnSave + // + this.btnSave.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text; + this.btnSave.ImageTransparentColor = System.Drawing.Color.Magenta; + this.btnSave.Name = "btnSave"; + this.btnSave.Size = new System.Drawing.Size(35, 22); + this.btnSave.Text = "Save"; + this.btnSave.Click += new System.EventHandler(this.BtnSave_Click); + // + // sfd + // + this.sfd.Filter = "PRG-files|*.prg"; // // FormC64Screen // @@ -149,5 +181,9 @@ private void InitializeComponent() { private System.Windows.Forms.ToolStripButton btnCopyScaledScreenBuffer; private System.Windows.Forms.ToolStripButton btnUseCrtFilter; private System.Windows.Forms.ToolStripButton btnRestart; + private System.Windows.Forms.ToolStripButton btnOpen; + private System.Windows.Forms.OpenFileDialog ofd; + private System.Windows.Forms.ToolStripButton btnSave; + private System.Windows.Forms.SaveFileDialog sfd; } } \ No newline at end of file diff --git a/ComputerSystems/Commodore64/FormC64Screen.cs b/ComputerSystems/Commodore64/FormC64Screen.cs index ba381e6..088f1c9 100644 --- a/ComputerSystems/Commodore64/FormC64Screen.cs +++ b/ComputerSystems/Commodore64/FormC64Screen.cs @@ -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 { @@ -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(); + + 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()); + } + } } } diff --git a/ComputerSystems/Commodore64/FormC64Screen.resx b/ComputerSystems/Commodore64/FormC64Screen.resx index 25001c6..cb91b15 100644 --- a/ComputerSystems/Commodore64/FormC64Screen.resx +++ b/ComputerSystems/Commodore64/FormC64Screen.resx @@ -123,4 +123,10 @@ 232, 17 + + 333, 17 + + + 403, 17 + \ No newline at end of file