cap5lut
cap5lut
CC#
Created by Pdawg on 11/17/2024 in #help
Micro-optimizing a Z80 emulators' pipeline. **Unsafe code**
indeed xD
215 replies
CC#
Created by Pdawg on 11/17/2024 in #help
Micro-optimizing a Z80 emulators' pipeline. **Unsafe code**
yeah on its own it looks quite big, but the actual inlined code is much smaller, if u use constants:
public class Cpu
{
private RegisterSet _registers = new();

public void LD_R_R(ref byte dst, ref byte src) => dst = src;
public void LD_R_R(ref ushort dst, ref ushort src) => dst = src;

public void LD_C_A() => LD_R_R(ref _registers.C, ref _registers.A);

public void Test()
{
LD_R_R(ref _registers.GetR16FromIndex(1), ref _registers.GetR16FromIndex(2));
}
}
public class Cpu
{
private RegisterSet _registers = new();

public void LD_R_R(ref byte dst, ref byte src) => dst = src;
public void LD_R_R(ref ushort dst, ref ushort src) => dst = src;

public void LD_C_A() => LD_R_R(ref _registers.C, ref _registers.A);

public void Test()
{
LD_R_R(ref _registers.GetR16FromIndex(1), ref _registers.GetR16FromIndex(2));
}
}
Cpu.Test()
L0000: add ecx, 4
L0003: mov eax, ecx
L0005: cmp [eax], al
L0007: add eax, 2
L000a: movzx edx, word ptr [ecx+4]
L000e: mov [eax], dx
L0011: ret
Cpu.Test()
L0000: add ecx, 4
L0003: mov eax, ecx
L0005: cmp [eax], al
L0007: add eax, 2
L000a: movzx edx, word ptr [ecx+4]
L000e: mov [eax], dx
L0011: ret
215 replies
CC#
Created by Pdawg on 11/17/2024 in #help
Micro-optimizing a Z80 emulators' pipeline. **Unsafe code**
but iirc then u will really have to do all 256 cases
215 replies
CC#
Created by Pdawg on 11/17/2024 in #help
Micro-optimizing a Z80 emulators' pipeline. **Unsafe code**
if u have methods like LD_C_A that call LD_R_R internally, it might be better if there is no inlining, because then its basically all about keeping this in a register and the switch will become a hug jump table
215 replies
CC#
Created by Pdawg on 11/17/2024 in #help
Micro-optimizing a Z80 emulators' pipeline. **Unsafe code**
well, since u will have mostly a switch over almost the whole byte range, i doubt there will be much inlining
215 replies
CC#
Created by Pdawg on 11/17/2024 in #help
Micro-optimizing a Z80 emulators' pipeline. **Unsafe code**
and now esp is actually the Cpu this
215 replies
CC#
Created by Pdawg on 11/17/2024 in #help
Micro-optimizing a Z80 emulators' pipeline. **Unsafe code**
lots of optimizations/inlining done there
Cpu.LD_R_R(Byte ByRef, Byte ByRef)
L0000: mov eax, [esp+4]
L0004: movzx eax, byte ptr [eax]
L0007: mov [edx], al
L0009: ret 4

Cpu.LD_C_A()
L0000: movzx eax, byte ptr [ecx+5]
L0004: mov [ecx+6], al
L0007: ret
Cpu.LD_R_R(Byte ByRef, Byte ByRef)
L0000: mov eax, [esp+4]
L0004: movzx eax, byte ptr [eax]
L0007: mov [edx], al
L0009: ret 4

Cpu.LD_C_A()
L0000: movzx eax, byte ptr [ecx+5]
L0004: mov [ecx+6], al
L0007: ret
215 replies
CC#
Created by Pdawg on 11/17/2024 in #help
Micro-optimizing a Z80 emulators' pipeline. **Unsafe code**
since i made it a method that takes RegisterSet as a parameter, i guess due to managed calling convention the struct is spilled onto the stack and esp gets a pointer to that, before the actual call happens
215 replies
CC#
Created by Pdawg on 11/17/2024 in #help
Micro-optimizing a Z80 emulators' pipeline. **Unsafe code**
byte ptr [esp+5] is ref registers.A
215 replies
CC#
Created by Pdawg on 11/17/2024 in #help
Micro-optimizing a Z80 emulators' pipeline. **Unsafe code**
L0000: mov byte ptr [esp+5], 0xff
L0005: movzx eax, byte ptr [esp+5]
L000a: inc eax
L000b: mov [esp+5], al
L0000: mov byte ptr [esp+5], 0xff
L0005: movzx eax, byte ptr [esp+5]
L000a: inc eax
L000b: mov [esp+5], al
thats basically the asm for
ref var reg = ref registers.A;
reg = 255;
reg++;
ref var reg = ref registers.A;
reg = 255;
reg++;
215 replies
CC#
Created by Pdawg on 11/17/2024 in #help
Micro-optimizing a Z80 emulators' pipeline. **Unsafe code**
if u look at this modified sharplab, u can see in the asm, that Unsafe.Add will be resolved to a constant offset. Unsafe.As doesnt even emit any code, thats purely "meta info" for roslyn and the JIT
215 replies
CC#
Created by Pdawg on 11/17/2024 in #help
Micro-optimizing a Z80 emulators' pipeline. **Unsafe code**
what i can say is, due to mostly handling refs, the JIT can probably optimize better, because it doesnt really have to look into what happens with the value u read and where it ends up
215 replies
CC#
Created by Pdawg on 11/17/2024 in #help
Micro-optimizing a Z80 emulators' pipeline. **Unsafe code**
i have no clue if its faster at all 😂
215 replies
CC#
Created by Pdawg on 11/17/2024 in #help
Micro-optimizing a Z80 emulators' pipeline. **Unsafe code**
well, makes sense for little endian
215 replies
CC#
Created by Pdawg on 11/17/2024 in #help
Micro-optimizing a Z80 emulators' pipeline. **Unsafe code**
the 8 bit registers might be in wrong order there btw 😂
215 replies
CC#
Created by Pdawg on 11/17/2024 in #help
Micro-optimizing a Z80 emulators' pipeline. **Unsafe code**
but the actual instruction is something like LD C, A, so u could do something like
public void LD_R_R(ref byte dest, ref byte source) => dest = source;
public void LD_R_R(ref byte dest, ref byte source) => dest = source;
and call case <opcode>: LD_R_R(ref _registers.C, ref _registers.A); break;
215 replies
CC#
Created by Pdawg on 11/17/2024 in #help
Micro-optimizing a Z80 emulators' pipeline. **Unsafe code**
yeah, all 3 properties access the same ushort
[UnscopedRef] public ref ushort HL => ref _hl;
[UnscopedRef] public ref byte H => ref Unsafe.Add(ref L, 1);
[UnscopedRef] public ref byte L => ref Unsafe.As<ushort, byte>(ref HL);
[UnscopedRef] public ref ushort HL => ref _hl;
[UnscopedRef] public ref byte H => ref Unsafe.Add(ref L, 1);
[UnscopedRef] public ref byte L => ref Unsafe.As<ushort, byte>(ref HL);
215 replies
CC#
Created by Pdawg on 11/17/2024 in #help
Micro-optimizing a Z80 emulators' pipeline. **Unsafe code**
i had ref byte properties for the 8 bit registers as well
215 replies
CC#
Created by Pdawg on 11/17/2024 in #help
Micro-optimizing a Z80 emulators' pipeline. **Unsafe code**
whats the current code of GetR16FromHighIndexer?
215 replies
CC#
Created by Pdawg on 11/17/2024 in #help
Micro-optimizing a Z80 emulators' pipeline. **Unsafe code**
doesnt seem too shabby 😄
215 replies