Skip to content

Commit

Permalink
Implement pad_poll() method
Browse files Browse the repository at this point in the history
Not 100% working in a sample yet.

Co-authored-by: Mike Corsaro <[email protected]>
Co-authored-by: Mike Corsaro <[email protected]>
  • Loading branch information
3 people committed Sep 19, 2024
1 parent df3d1ab commit cdee779
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 12 deletions.
13 changes: 4 additions & 9 deletions src/dotnes.tasks/Utilities/IL2NESWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,14 +370,8 @@ ushort GetAddress(string name)
case nameof(scroll):
return 0x82FB;
case nameof(oam_spr):
if (Labels.TryGetValue(nameof(oam_spr), out var address))
{
return address;
}
else
{
return 0x0000;
}
case nameof(pad_poll):
return Labels.TryGetValue(name, out var address) ? address : (ushort)0;
default:
throw new NotImplementedException($"{nameof(GetAddress)} for {name} is not implemented!");
}
Expand Down Expand Up @@ -407,9 +401,10 @@ static int GetNumberOfArguments(string name)
case nameof(pal_spr_bright):
case nameof(pal_spr):
case nameof(pal_all):
case nameof(set_rand):
case nameof(set_rand):
case nameof(oam_hide_rest):
case nameof(oam_size):
case nameof(pad_poll):
return 1;
case nameof(pal_col):
case nameof(vram_fill):
Expand Down
31 changes: 28 additions & 3 deletions src/dotnes.tasks/Utilities/NESInstruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ enum NESInstruction : byte
/// Set Carry Flag
/// </summary>
SEC_impl = 0x38,
//TODO: rest of 3
/// <summary>
/// AND Memory with Accumulator
/// </summary>
AND_Y_abs = 0x39,
//TODO: rest of them

/// <summary>
/// Return from Interrupt
Expand All @@ -126,7 +130,16 @@ enum NESInstruction : byte
/// Push Accumulator on Stack
/// </summary>
PHA_impl = 0x48,
//TODO: 4-5

/// <summary>
/// Logical shift right
/// </summary>
LSR_impl = 0x4A,

/// <summary>
/// Exclusive-OR Memory with Accumulator
/// </summary>
EOR_Y_abs = 0x59,

// 6

Expand Down Expand Up @@ -173,9 +186,17 @@ enum NESInstruction : byte

//TODO: 7
/// <summary>
/// Rotate One Bit Right (Memory or Accumulator)
/// </summary>
ROR_X_zpg = 0x76,
/// <summary>
/// Set Interrupt Disable Status
/// </summary>
SEI_impl = 0x78,
/// <summary>
/// Rotate One Bit Right (Memory or Accumulator)
/// </summary>
ROR_X_abs = 0x7E,

/// <summary>
/// Store Accumulator in Memory
Expand Down Expand Up @@ -232,7 +253,11 @@ enum NESInstruction : byte
/// Transfer Index Y to Accumulator
/// </summary>
TYA_impl = 0x98,
//TODO: rest of 9
/// <summary>
/// Store Accumulator in Memory
/// </summary>
STA_abs_Y = 0x99,
//TODO: rest of them
/// <summary>
/// 9D: Store Accumulator in Memory
/// </summary>
Expand Down
78 changes: 78 additions & 0 deletions src/dotnes.tasks/Utilities/NESWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,87 @@ public void WriteFinalBuiltIns(ushort totalSize, byte locals)
{
WriteBuiltIn(nameof(NESLib.oam_spr), totalSize);
}
if (UsedMethods.Contains(nameof(NESLib.pad_poll)))
{
Write_pad_poll();
}
}
}

void Write_pad_poll()
{
SetLabel(nameof(NESLib.pad_poll), (ushort)(_writer.BaseStream.Position + BaseAddress));
/*
* 8598 A8 TAY ; _pad_poll
* 8599 A200 LDX #$00
* 859B A901 LDA #$01 ; @padPollPort
* 859D 8D1640 STA $4016
* 85A0 A900 LDA #$00
* 85A2 8D1640 STA $4016
* 85A5 A908 LDA #$08
* 85A7 8517 STA TEMP
*
* 85A9 B91640 LDA $4016,y ; @padPollLoop
* 85AC 4A LSR
* 85AD 7618 ROR TEMP+1,x
* 85AF C617 DEC TEMP
* 85B1 D0F6 BNE @padPollLoop
* 85B3 E8 INX
* 85B4 E003 CPX #$03
* 85B6 D0E3 BNE @padPollPort
* 85B8 A518 LDA TEMP+1
* 85BA C519 CMP $19
* 85BC F006 BEQ @done
* 85BE C51A CMP $1A
* 85C0 F002 BEQ @done
* 85C2 A519 LDA $19
*
* 85C4 993C00 STA PAD_STATE,y ; @done
* 85C7 AA TAX
* 85C8 593E00 EOR PAD_STATEP,y
* 85CB 393C00 AND PAD_STATE,y
* 85CE 994000 STA PAD_STATET,y
* 85D1 8A TXA
* 85D2 993E00 STA PAD_STATEP,y
* 85D5 A200 LDX #$00
* 85D7 60 RTS
*/
Write(NESInstruction.TAY_impl);
Write(NESInstruction.LDX, 0x00);
// Pad poll port
Write(NESInstruction.LDA, 0x01);
Write(NESInstruction.STA_abs, 0x4016);
Write(NESInstruction.LDA, 0x00);
Write(NESInstruction.STA_abs, 0x4016);
Write(NESInstruction.LDA, 0x08);
Write(NESInstruction.STA_zpg, TEMP);
// Pad poll loop
Write(NESInstruction.LDA_abs_y, 0x4016);
Write(NESInstruction.LSR_impl);
Write(NESInstruction.ROR_X_zpg, TEMP + 1);
Write(NESInstruction.DEC_zpg, TEMP);
Write(NESInstruction.BNE_rel, 0xF6); // Pad poll loop
Write(NESInstruction.INX_impl);
Write(NESInstruction.CPX, 0x03);
Write(NESInstruction.BNE_rel, 0xE3); // Pad poll port
Write(NESInstruction.LDA_zpg, TEMP + 1);
Write(NESInstruction.CMP_zpg, 0x19);
Write(NESInstruction.BEQ_rel, 0x06);
Write(NESInstruction.CMP_zpg, 0x1A);
Write(NESInstruction.BEQ_rel, 0x02); // Done
Write(NESInstruction.LDA_zpg, @byte: 0x19);
// Pad done
Write(NESInstruction.STA_abs_Y, (ushort)0x003C);
Write(NESInstruction.TAX_impl);
Write(NESInstruction.EOR_Y_abs, (ushort)0x003E);
Write(NESInstruction.AND_Y_abs, (ushort)0x003C);
Write(NESInstruction.STA_abs_Y, (ushort)0x0040);
Write(NESInstruction.TXA_impl);
Write(NESInstruction.STA_abs_Y, (ushort)0x003E);
Write(NESInstruction.LDX, 0x0);
Write(NESInstruction.RTS_impl);
}

/// <summary>
/// Writes a built-in method from NESLib
/// </summary>
Expand Down

0 comments on commit cdee779

Please sign in to comment.