Skip to content

Commit

Permalink
Saving to SD card.
Browse files Browse the repository at this point in the history
  • Loading branch information
dtremblay committed Dec 29, 2020
1 parent 1bc14e2 commit f62bf7d
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 71 deletions.
99 changes: 64 additions & 35 deletions Main/Devices/SDCard/GabeSDController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public class GabeSDController : SDCardDevice
private string spaces = " ";
private bool mbrPresent = false;

int writeStartCluster = 0; // this is used when writing to file, to determine the cluster number
FileEntry voidEntry = null; // this is a "newfile" entry that gets used when creating a new file.


Expand Down Expand Up @@ -174,7 +173,7 @@ public override void WriteByte(int Address, byte Value)
int writeAddress = ReadWord(7) + (ReadWord(9) << 16);
if (GetISOMode())
{
SetData_ISO((writeAddress - BOOT_SECTOR_ADDR) / 512, readBlock); // write the 512 Byte buffer
SetData_ISO((writeAddress - BOOT_SECTOR_ADDR) / 512, writeBlock); // write the 512 Byte buffer
}
else
{
Expand All @@ -189,11 +188,10 @@ public override void WriteByte(int Address, byte Value)
}
else if (writeAddress >= FAT_OFFSET_START && writeAddress <= FAT_OFFSET_START + FAT_SIZE - 1)
{
Console.WriteLine("Gabe is trying to write to FAT Area!");

// read the fat table
blockPtr = 0;
int fatPage = (writeAddress - FAT_OFFSET_START) / 512;
Console.WriteLine("Gabe is trying to write to FAT Area! Page: " + fatPage);
// Compare the last FAT with the writeBlock - based on how many clusters are created, we can determine the filesize
PrepareEmptyFileEntry(fatPage);
writeBlock = new byte[512];
Expand All @@ -208,23 +206,33 @@ public override void WriteByte(int Address, byte Value)
}
else if (writeAddress >= DATA_OFFSET_START && writeAddress <= DATA_OFFSET_START + DATA_SIZE - 1)
{
Console.WriteLine("Gabe is trying to write to Data Area!");

// read the data
int dataPage = (writeAddress - DATA_OFFSET_START) / 512;
if (GetFSType() == FSType.FAT32)
{
dataPage += ROOT_SIZE / 512;
dataPage += 2 + ROOT_SIZE / 512;
}
Console.WriteLine("Gabe is trying to write to Data Area! Cluster: " + dataPage);
SetData(dataPage, writeBlock);
blockPtr = 0;
}
else
{
// Invalid address
Console.WriteLine("Gabe is trying to write to an invalid address:" + writeAddress);
data[5] = 1;
}
}
// reset the write block
writeBlock = new byte[512];
break;
}
}
else
{
// set the error status
data[5] = 1;
}
break;
case MemoryMap.GABE_SDC_RX_FIFO_CTRL_REG - MemoryMap.GABE_SDC_CTRL_START:
data[5] = 0;
Expand Down Expand Up @@ -906,21 +914,38 @@ private byte[] GetData(int page)

private void SetData(int page, byte[] buffer)
{
FileStream stream = new FileStream(voidEntry.fqpn, FileMode.Open, FileAccess.Write);
try
{
stream.Seek((page + 2 - writeStartCluster) * 512, SeekOrigin.Begin);
stream.Write(buffer, 0, 512);
}
catch (Exception e)
// Find the file in FAT
FileEntry fEntry = null;
int writeStartCluster = 0;
foreach (int key in FAT.Keys)
{
// controller error
data[5] = 1;
System.Console.WriteLine(e.ToString());
FileEntry entry = FAT[key];
if (page >= key && page <= key + entry.clusters)
{
fEntry = entry;
writeStartCluster = key;
break;
}
}
finally

if (fEntry != null)
{
stream.Close();
FileStream stream = new FileStream(fEntry.fqpn, FileMode.Open, FileAccess.Write);
try
{
stream.Seek((page - writeStartCluster) * 512, SeekOrigin.Begin);
stream.Write(buffer, 0, 512);
}
catch (Exception e)
{
// controller error
data[5] = 1;
System.Console.WriteLine(e.ToString());
}
finally
{
stream.Close();
}
}
}

Expand All @@ -929,29 +954,32 @@ private void SetData(int page, byte[] buffer)
private void PrepareEmptyFileEntry(int page)
{
int ffCntr = 0;
for (int i = 0; i < 512; i++)
if (firstPtr == -1 && lastPtr == -1)
{
if (firstPtr == -1)
for (int i = 0; i < 512; i++)
{
if (fat[i] != writeBlock[i])
if (firstPtr == -1)
{
firstPtr = i;
if (writeBlock[i] == 0xFF)
if (fat[i] != writeBlock[i])
{
ffCntr++;
firstPtr = i;
if (writeBlock[i] == 0xFF)
{
ffCntr++;
}
}
}
}
else
{
if (i % 4 != 3 && writeBlock[i] == 0xff)
{
ffCntr++;
}
if (ffCntr == 3 && i %4 ==3 && writeBlock[i] == 0x0f)
else
{
lastPtr = i;
break;
if (i % 4 != 3 && writeBlock[i] == 0xff)
{
ffCntr++;
}
if (ffCntr == 3 && i % 4 == 3 && writeBlock[i] == 0x0f)
{
lastPtr = i;
break;
}
}
}
}
Expand Down Expand Up @@ -991,6 +1019,7 @@ private void CreateEmptyFile(int size, int page)


// calculate the first cluster offset
int writeStartCluster = 0; // this is used when writing to file, to determine the cluster number
switch (GetFSType())
{
case FSType.FAT16:
Expand Down
37 changes: 13 additions & 24 deletions Main/Display/Gpu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public Point GetScreenSize()

const int STRIDE = 800;
Bitmap frameBuffer = new Bitmap(STRIDE, 600, PixelFormat.Format32bppArgb);
private volatile bool drawing = false;
private bool drawing = false;

/// <summary>
/// Draw the frame buffer to the screen.
Expand All @@ -158,21 +158,7 @@ unsafe void Gpu_Paint(object sender, PaintEventArgs e)
e.Graphics.DrawString("Design Mode", this.Font, TextBrush, 0, 0);
return;
}
if (VICKY == null)
{
e.Graphics.DrawString("IO Memory Not Initialized", this.Font, TextBrush, 0, 0);
return;
}
if (VRAM == null)
{
e.Graphics.DrawString("VRAM Not Initialized", this.Font, TextBrush, 0, 0);
return;
}
if (RAM == null)
{
e.Graphics.DrawString("RAM Not Initialized", this.Font, TextBrush, 0, 0);
return;
}

// Read the Master Control Register
byte MCRegister = VICKY.ReadByte(0); // Reading address $AF:0000
byte MCRHigh = (byte)(VICKY.ReadByte(1) & 3); // Reading address $AF:0001
Expand Down Expand Up @@ -457,7 +443,7 @@ public static int[] LoadLUT(MemoryRAM VKY)
}

// We only cache items that are requested, instead of precomputing all 1024 colors.
private int getLUTValue(byte lutIndex, byte color, bool gamma)
private int GetLUTValue(byte lutIndex, byte color, bool gamma)
{
int offset = lutIndex * 256 + color;
int value = lutCache[offset];
Expand All @@ -484,7 +470,7 @@ private int getLUTValue(byte lutIndex, byte color, bool gamma)

int[] FGTextLUT;
int[] BGTextLUT;
private int[] getTextLUT(byte fg, byte bg, bool gamma)
private int[] GetTextLUT(byte fg, byte bg, bool gamma)
{
int[] values = new int[2];
if (FGTextLUT[fg] == 0)
Expand Down Expand Up @@ -591,7 +577,7 @@ private unsafe void DrawBitmapText(int* p, int MCR, bool gammaCorrection, byte T
byte fgColor = (byte)((color & 0xF0) >> 4);
byte bgColor = (byte)(color & 0x0F);

int[] textColors = getTextLUT(fgColor, bgColor, gammaCorrection);
int[] textColors = GetTextLUT(fgColor, bgColor, gammaCorrection);

byte value = VICKY.ReadByte(fontBaseAddress + character * 8 + fontLine);
//int offset = (x + line * 640) * 4;
Expand Down Expand Up @@ -636,11 +622,14 @@ private unsafe void DrawBitmap(int* p, bool gammaCorrection, int layer, bool bkg
int pixelOffset = line * STRIDE;
int* ptr = p + pixelOffset;
int col = borderXSize;
byte pixVal = 0;
//byte pixVal = 0;

byte[] pixVals = new byte[width];
VRAM.CopyIntoBuffer(offsetAddress, pixVals, 0, width);
while (col < width - borderXSize)
{
pixVal = VRAM.ReadByte(offsetAddress + col);
colorVal = pixVal == 0 ? bgndColor : getLUTValue(lutIndex, pixVal, gammaCorrection);
//pixVal = VRAM.ReadByte(offsetAddress + col);
colorVal = pixVals[col] == 0 ? bgndColor : GetLUTValue(lutIndex, pixVals[col], gammaCorrection);
ptr[col++] = colorVal;
}
}
Expand Down Expand Up @@ -723,7 +712,7 @@ private unsafe void DrawTiles(int* p, bool gammaCorrection, byte TextColumns, in
byte pixelIndex = VRAM.ReadByte(tilesetOffsetAddress);
if (pixelIndex > 0)
{
int value = getLUTValue(tileLUT, pixelIndex, gammaCorrection);
int value = GetLUTValue(tileLUT, pixelIndex, gammaCorrection);
ptr[x] = value;
}
}
Expand Down Expand Up @@ -789,7 +778,7 @@ private unsafe void DrawSprites(int* p, bool gammaCorrection, int layer, bool bk
pixelIndex = VRAM.ReadByte(spriteAddress + col + sline * 32);
if (pixelIndex != 0)
{
value = getLUTValue(lutIndex, pixelIndex, gammaCorrection);
value = GetLUTValue(lutIndex, pixelIndex, gammaCorrection);

//System.Runtime.InteropServices.Marshal.WriteInt32(p, (lineOffset + (col-xOffset + posX)) * 4, value);
ptr[col - xOffset + posX] = value;
Expand Down
4 changes: 2 additions & 2 deletions Main/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.5.3.7")]
[assembly: AssemblyFileVersion("0.5.3.7")]
[assembly: AssemblyVersion("0.5.3.8")]
[assembly: AssemblyFileVersion("0.5.3.8")]
22 changes: 14 additions & 8 deletions Main/UI/CPUWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,11 @@ private void StepButton_Click(object sender, EventArgs e)
private void RefreshStatus()
{
this.Text = "Debug: " + StepCounter.ToString();
DebugPanel.Refresh();
UpdateStackDisplay();
if (kernel.CPU.DebugPause)
{
DebugPanel.Refresh();
UpdateStackDisplay();
}
registerDisplay1.UpdateRegisters();
}

Expand Down Expand Up @@ -600,7 +603,10 @@ public void ExecuteStep()
}
return;
}
if (knl_breakpoints.ContainsKey(nextPC) || (BreakOnIRQCheckBox.Checked && ((kernel.CPU.Pins.GetInterruptPinActive && InterruptMatchesCheckboxes()) || kernel.CPU.CurrentOpcode.Value == 0)))
if ( knl_breakpoints.ContainsKey(nextPC) ||
kernel.CPU.CurrentOpcode.Value == 0 ||
(BreakOnIRQCheckBox.Checked && (kernel.CPU.Pins.GetInterruptPinActive && InterruptMatchesCheckboxes()) )
)
{
if (UpdateTraceTimer.Enabled || kernel.CPU.CurrentOpcode.Value == 0)
{
Expand Down Expand Up @@ -813,26 +819,26 @@ private bool InterruptMatchesCheckboxes()
{
// Read Interrupt Register 0
byte reg0 = kernel.MemMgr.INTERRUPT.ReadByte(0);
if ((reg0 & (byte)Register0.FNX0_INT00_SOF) != 0 && SOFCheckbox.Checked)
if (SOFCheckbox.Checked && (reg0 & (byte)Register0.FNX0_INT00_SOF) != 0)
{
return true;
}
if ((reg0 & (byte)Register0.FNX0_INT01_SOL) != 0 && SOLCheckbox.Checked)
if (SOLCheckbox.Checked && (reg0 & (byte)Register0.FNX0_INT01_SOL) != 0)
{
return true;
}
if ((reg0 & (byte)Register0.FNX0_INT07_MOUSE) != 0 && MouseCheckbox.Checked)
if (MouseCheckbox.Checked && (reg0 & (byte)Register0.FNX0_INT07_MOUSE) != 0)
{
return true;
}

// Read Interrupt Register 1
byte reg1 = kernel.MemMgr.INTERRUPT.ReadByte(1);
if ((reg1 & (byte)Register1.FNX1_INT07_SDCARD ) != 0 && SDCardCheckBox.Checked)
if (SDCardCheckBox.Checked && (reg1 & (byte)Register1.FNX1_INT07_SDCARD ) != 0)
{
return true;
}
if ((reg1 & (byte)Register1.FNX1_INT00_KBD) != 0 && KeyboardCheckBox.Checked)
if (KeyboardCheckBox.Checked && (reg1 & (byte)Register1.FNX1_INT00_KBD) != 0)
{
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions Main/UI/MainWindow.Designer.cs

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

Binary file modified bin/Release/FoenixIDE.exe
Binary file not shown.

0 comments on commit f62bf7d

Please sign in to comment.