Skip to content

Commit

Permalink
console
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertvanheusden committed Feb 26, 2025
1 parent 254a8b2 commit f08498b
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 33 deletions.
82 changes: 61 additions & 21 deletions floppy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,46 @@ class FloppyDisk : Device
private int [] _cylinder = new int[4];
private int [] _head = new int[4];
private int _cylinder_seek_result = 0;
private static readonly System.Threading.Lock _filenames_lock = new();

public FloppyDisk(List<string> filenames)
{
Console.WriteLine("Floppy drive instantiated");
_filenames = filenames;
}

public int GetUnitCount()
{
return _filenames.Count();
}

public string GetUnitFilename(int unit)
{
lock(_filenames_lock)
{
if (unit < _filenames.Count())
{
return _filenames[unit];
}
}

return null;
}

public bool SetUnitFilename(int unit, string filename)
{
lock(_filenames_lock)
{
if (unit < _filenames.Count())
{
_filenames[unit] = filename;
return true;
}
}

return false;
}

public override int GetIRQNumber()
{
return _irq_nr;
Expand Down Expand Up @@ -123,29 +156,33 @@ public override (byte, bool) IO_Read(ushort port)

private byte [] GetFromFloppyImage(int unit, int cylinder, int head, int sector, int n)
{
long file_size = new System.IO.FileInfo(_filenames[unit]).Length;
int sectors_per_track = 9;
if (file_size >= 819200)
sectors_per_track = 18;

if (sector > sectors_per_track)
Log.DoLog($"Floppy-ReadData: reading beyond sector-count? ({sector} > {sectors_per_track})");

byte[] b = new byte[256 * n];
int lba = (cylinder * 2 + head) * sectors_per_track + sector - 1;
long offset = lba * b.Length;
Log.DoLog($"Floppy-ReadData LBA {lba}, offset {offset}, n {n} C {cylinder} H {head} S {sector} ({sectors_per_track})", true);

for(int nr=0; nr<n; nr++)
lock(_filenames_lock)
{
using (FileStream fs = File.Open(_filenames[unit], FileMode.Open, FileAccess.Read, FileShare.None))
long file_size = new System.IO.FileInfo(_filenames[unit]).Length;
int sectors_per_track = 9;
if (file_size >= 819200)
sectors_per_track = 18;

if (sector > sectors_per_track)
Log.DoLog($"Floppy-ReadData: reading beyond sector-count? ({sector} > {sectors_per_track})");

int lba = (cylinder * 2 + head) * sectors_per_track + sector - 1;
long offset = lba * b.Length;
Log.DoLog($"Floppy-ReadData LBA {lba}, offset {offset}, n {n} C {cylinder} H {head} S {sector} ({sectors_per_track})", true);

for(int nr=0; nr<n; nr++)
{
fs.Seek(offset, SeekOrigin.Begin);
if (fs.Read(b, 256 * nr, 256) != 256)
Log.DoLog($"Floppy-ReadData failed reading from backend ({_filenames[unit]}, offset: {offset})", true);
if (fs.Position != offset + 256)
Log.DoLog($"Floppy-ReadData backend data processing error?", true);
offset += 256;
using (FileStream fs = File.Open(_filenames[unit], FileMode.Open, FileAccess.Read, FileShare.None))
{
fs.Seek(offset, SeekOrigin.Begin);
if (fs.Read(b, 256 * nr, 256) != 256)
Log.DoLog($"Floppy-ReadData failed reading from backend ({_filenames[unit]}, offset: {offset})", true);
if (fs.Position != offset + 256)
Log.DoLog($"Floppy-ReadData backend data processing error?", true);
offset += 256;
}
}
}

Expand All @@ -154,8 +191,11 @@ private byte [] GetFromFloppyImage(int unit, int cylinder, int head, int sector,

private bool ReadData(int unit)
{
if (unit >= _filenames.Count())
return false;
lock(_filenames_lock)
{
if (unit >= _filenames.Count())
return false;
}

int sector = _data[4];
int head = (_data[1] & 4) == 4 ? 1 : 0;
Expand Down
57 changes: 45 additions & 12 deletions main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,7 @@
Console.Write("==>");

string line = Console.ReadLine();

//Log.DoLog(line);
Log.DoLog(line, true);

string[] parts = line.Split(' ');

Expand Down Expand Up @@ -399,9 +398,7 @@
{
p.ResetCrashCounter();

while(p.Tick())
{
}
runner(p);
}
else if (line != "")
{
Expand All @@ -413,6 +410,49 @@
}
else
{
Thread thread = new Thread(runner);
thread.Name = "runner";
thread.Start(p);

for(;;)
{
Console.Write("==>");

string line = Console.ReadLine();
Log.DoLog(line, true);
if (line == "")
continue;

if (line == "quit")
break;

if (line == "help")
{
Console.WriteLine("quit terminate application");
// Console.WriteLine("lsfloppy list configured floppies");
// Console.WriteLine("setfloppy x y set floppy unit x (0 based) to file y");
}
// else if (line == "lsfloppy")
// {
// }
else
{
Console.WriteLine($"\"{line}\" is not understood");
}
}
}

Log.EmitDisassembly();

if (test != "" && mode == TMode.Binary)
System.Environment.Exit(p.GetSI() == 0xa5ee ? 123 : 0);

System.Environment.Exit(0);

void runner(object p_in)
{
P8086 p = (P8086)p_in;

try
{
long prev_time = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
Expand Down Expand Up @@ -441,10 +481,3 @@
Log.DoLog(msg);
}
}

Log.EmitDisassembly();

if (test != "" && mode == TMode.Binary)
System.Environment.Exit(p.GetSI() == 0xa5ee ? 123 : 0);

System.Environment.Exit(0);

0 comments on commit f08498b

Please sign in to comment.