Skip to content

Commit 99cc345

Browse files
authored
Refactor TapeManager and related classes: Consolidate tape management logic, improve encapsulation, and standardize method naming. (#29)
1 parent fef6519 commit 99cc345

File tree

7 files changed

+54
-23
lines changed

7 files changed

+54
-23
lines changed

src/Spectron.Emulation/Emulator.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ public bool IsFloatingBusEnabled
6262

6363
public ComputerType ComputerType { get; }
6464
public RomType RomType { get; }
65-
public TapeSpeed TapeLoadSpeed { get; set; }
6665

6766
public Z80 Cpu { get; }
6867
public IEmulatorMemory Memory => _memory;
@@ -310,7 +309,7 @@ private void BeforeInstruction(Word pc)
310309
switch (pc)
311310
{
312311
case RomRoutines.LD_START:
313-
switch (TapeLoadSpeed)
312+
switch (TapeManager.TapeLoadSpeed)
314313
{
315314
case TapeSpeed.Instant:
316315
TapeManager.FastLoad();

src/Spectron.Emulation/Tape/Cassette.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,29 @@ public sealed class Cassette
3131
public event BlockSelectedEvent? BlockSelected;
3232
public event EventHandler? EndOfTape;
3333

34-
internal void Load(Stream stream, FileType fileType)
34+
internal void SetContent(Stream stream, FileType fileType)
3535
{
3636
Position = 0;
3737

3838
switch (fileType)
3939
{
4040
case FileType.Tap:
4141
var tapFile = TapFile.Load(stream);
42-
Load(tapFile.ToTzx());
42+
SetContent(tapFile.ToTzx());
4343

4444
break;
4545

4646
case FileType.Tzx:
4747
var tzxFile = TzxFile.Load(stream);
48-
Load(tzxFile);
48+
SetContent(tzxFile);
4949

5050
break;
5151
}
5252

5353
_contentBytes = null;
5454
}
5555

56-
internal void Load(TzxFile tzxFile, int currentPosition = 0)
56+
internal void SetContent(TzxFile tzxFile, int currentPosition = 0)
5757
{
5858
Position = currentPosition;
5959
Content = tzxFile;

src/Spectron.Emulation/Tape/DirectAccess.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ internal DirectAccess(Z80 cpu, IMemory memory)
2424
internal void FastLoad(Cassette cassette)
2525
{
2626
var tap = cassette.GetNextTapData();
27+
2728
if (tap == null)
2829
{
2930
return;
@@ -77,7 +78,7 @@ internal void FastLoad(Cassette cassette)
7778
_cpu.Registers.PC = 0x05E0; // CP $01; RET
7879
}
7980

80-
internal void FastSave(Cassette cassette, TapeSpeed speed)
81+
internal void FastSave(Cassette cassette)
8182
{
8283
var blockType = _cpu.Registers.A;
8384
var length = _cpu.Registers.DE;
@@ -88,9 +89,6 @@ internal void FastSave(Cassette cassette, TapeSpeed speed)
8889

8990
cassette.Content.Blocks.Add(new StandardSpeedDataBlock(tapData));
9091

91-
if (speed == TapeSpeed.Instant)
92-
{
93-
_cpu.Registers.PC = RomRoutines.SA_DELAY - 2;
94-
}
92+
_cpu.Registers.PC = RomRoutines.SA_DELAY - 2;
9593
}
9694
}

src/Spectron.Emulation/Tape/TapeManager.cs

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,23 @@ public sealed class TapeManager
1313
{
1414
private DirectAccess? _directAccess;
1515

16-
public Cassette Cassette { get; private set; } = new();
16+
public Cassette Cassette { get; private set; }
1717

1818
public bool IsTapeLoaded { get; private set; }
1919
public bool IsTapeSaveEnabled { get; set; }
20+
21+
public TapeSpeed TapeLoadSpeed { get; set; }
2022
public TapeSpeed TapeSaveSpeed { get; set; }
23+
2124
public double BlockReadProgressPercentage => CassettePlayer?.BlockReadProgressPercentage ?? 0;
2225

2326
internal CassettePlayer? CassettePlayer { get; private set; }
2427

2528
public delegate void TapeStateChangedEvent(TapeStateEventArgs e);
2629
public event TapeStateChangedEvent? TapeStateChanged;
2730

31+
public TapeManager() => Cassette = CreateCassette();
32+
2833
internal void Attach(Z80 cpu, IMemory memory, HardwareSettings hardware)
2934
{
3035
CassettePlayer = new CassettePlayer(Cassette, cpu.Clock, hardware);
@@ -35,7 +40,7 @@ internal void Attach(Z80 cpu, IMemory memory, HardwareSettings hardware)
3540

3641
public void NewTape()
3742
{
38-
Cassette = new Cassette();
43+
Cassette = CreateCassette();
3944

4045
TapeStateChanged?.Invoke(new TapeStateEventArgs(TapeAction.TapeInserted));
4146

@@ -56,14 +61,14 @@ public void FastSave()
5661
NewTape();
5762
}
5863

59-
_directAccess?.FastSave(Cassette, TapeSaveSpeed);
64+
_directAccess?.FastSave(Cassette);
6065
}
6166

6267
public void InsertTape(Stream stream, FileType fileType, bool autoPlay = false)
6368
{
6469
StopTape();
6570

66-
Cassette.Load(stream, fileType);
71+
Cassette.SetContent(stream, fileType);
6772
InsertTape();
6873

6974
if (autoPlay)
@@ -72,17 +77,17 @@ public void InsertTape(Stream stream, FileType fileType, bool autoPlay = false)
7277
}
7378
}
7479

75-
public void InsertTape(string fileName, bool autoPlay = false)
80+
public void InsertTape(string fileName)
7681
{
7782
var fileType = FileTypes.GetFileType(fileName);
7883
var stream = File.OpenRead(fileName);
7984

80-
InsertTape(stream, fileType, autoPlay);
85+
InsertTape(stream, fileType);
8186
}
8287

8388
public void InsertTape(TzxFile tzxFile, int currentBlockIndex)
8489
{
85-
Cassette.Load(tzxFile, currentBlockIndex);
90+
Cassette.SetContent(tzxFile, currentBlockIndex);
8691
InsertTape();
8792
}
8893

@@ -111,9 +116,38 @@ public void EjectTape()
111116
StopTape();
112117
TapeStateChanged?.Invoke(new TapeStateEventArgs(TapeAction.TapeEjected));
113118

114-
Cassette = new Cassette();
119+
Cassette = CreateCassette();
115120
IsTapeLoaded = false;
116121
}
117122

118123
public void RewindTape() => CassettePlayer?.Rewind();
124+
125+
private Cassette CreateCassette()
126+
{
127+
var cassette = new Cassette();
128+
129+
cassette.BlockSelected += CassetteOnBlockSelected;
130+
131+
return cassette;
132+
}
133+
134+
private void CassetteOnBlockSelected(BlockSelectedEventArgs e)
135+
{
136+
if (CassettePlayer?.IsPlaying == true)
137+
{
138+
return;
139+
}
140+
141+
if (e.Position == Cassette.Content.Blocks.Count - 1)
142+
{
143+
return;
144+
}
145+
146+
if (Cassette.Content.Blocks[e.Position + 1].BlockId == BlockCode.TurboSpeedData)
147+
{
148+
//PlayTape();
149+
}
150+
151+
Console.WriteLine($"Block selected: {e.Position}");
152+
}
119153
}

src/Spectron.Emulation/Tape/TapeSpeed.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace OldBit.Spectron.Emulation.Tape;
22

33
/// <summary>
4-
/// Specifies the speed at which the tape should be loaded or saved
4+
/// Specifies the speed at which the tape should be loaded or saved.
55
/// </summary>
66
public enum TapeSpeed
77
{

src/Spectron/Extensions/EmulatorExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static void SetTapeLoadingSpeed(this Emulator? emulator, TapeSpeed tapeSp
1515
return;
1616
}
1717

18-
emulator.TapeLoadSpeed = tapeSpeed;
18+
emulator.TapeManager.TapeLoadSpeed = tapeSpeed;
1919
}
2020

2121
public static void SetTapeSettings(this Emulator? emulator, TapeSettings tapeSettings)
@@ -27,7 +27,7 @@ public static void SetTapeSettings(this Emulator? emulator, TapeSettings tapeSet
2727

2828
emulator.TapeManager.IsTapeSaveEnabled = tapeSettings.IsSaveEnabled;
2929
emulator.TapeManager.TapeSaveSpeed = tapeSettings.SaveSpeed;
30-
emulator.TapeLoadSpeed = tapeSettings.LoadSpeed;
30+
emulator.TapeManager.TapeLoadSpeed = tapeSettings.LoadSpeed;
3131
}
3232

3333
public static void SetAudioSettings(this Emulator? emulator, AudioSettings audioSettings)

src/Spectron/ViewModels/MainWindowViewModel.Emulator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private void InitializeEmulator(Emulator emulator)
7878
MouseType = Emulator.MouseManager.MouseType;
7979
IsUlaPlusEnabled = Emulator.IsUlaPlusEnabled;
8080

81-
Emulator.TapeLoadSpeed = TapeLoadSpeed;
81+
Emulator.TapeManager.TapeLoadSpeed = TapeLoadSpeed;
8282
Emulator.FrameCompleted += EmulatorFrameCompleted;
8383

8484
ConfigureEmulatorSettings();

0 commit comments

Comments
 (0)