Skip to content

Add Cx4 support - #415

Open
Ersanio wants to merge 3 commits into
RPGHacker:asar_2_betafrom
Ersanio:asar_2_beta-cx4
Open

Ersanio wants to merge 3 commits into
RPGHacker:asar_2_betafrom
Ersanio:asar_2_beta-cx4

Conversation

@Ersanio

@Ersanio Ersanio commented Nov 20, 2026 •

Copy link
Copy Markdown

This update adds arch cx4 which assembles code for the Capcom Cx4 coprocessor (Hitachi HG51B169).

Assembler

See src/asar/arch-cx4.cpp. The convention for instructions with commas (e.g. LD A, R0) is <dest>, <src>.

"pending hardware confirmation"

arch-cx4.cpp mentions a lot of "pending hardware confirmation" in the comment. I do not know how the HG51B169 emulator core was developed, and what kind of test ROMs were used, so I based all of the logic and quirks on the bsnes emulator (78474e5) which I treated as the "source of truth".

I do eventually plan to make my own Cx4 development board to confirm these quirks, myself. Until then, the Cx4 assembler is "bsnes-compliant" (perhaps to a fault, see the quirk about storing into MACH).

Unusual syntax

  • Far jumps/calls are prefixed with a P:
  • Some opcodes support shifting A within the operation (e.g. ADD A << 8, #$42)
  • WRRAM and RDRAM expect a RAMB+<value> and a DPR+<value>
  • INC MAR is the only documented INC
  • IR0-IR15, can also be written as their #$xxxxxx values
  • CMP/SUB and their reversed variant CMPR/SUBR in both cases read as "left minus right"; The assembler takes care of the parameter reversing

Errors, warnings and other quirks

The assembler tries to hold the user's hand with warnings and errors for parameter combinations that don't make sense or simply won't work. e.g. ST A, A, is syntactically valid, but doesn't make sense semantically.

The assembler also warns the user about certain Cx4 quirks that are more obscure, such as writing a 24-bit value to MACH only storing the lower 8 bits (which might be due to a bsnes bug), or a shift count higher than 24 resulting in no shifting.

Docs and editor support

See docs/src/arch-cx4.md. It shows what registers you can work with, and what opcodes are available. A full documentation involving hardware registers, timings, pinout etc. is out of scope for asar. The syntax highlighters have also been updated. The only exception is Sublime Text; it supports 65c816 only, so I kept it that way.

Example project

I have an example project over at one of my own repositories. It's meant to run on bsnes along with the .bml file. It improves the level loading times by optimizing the LC_LZ2 decompression, so you'll know the patch applied successfully if level loads are faster.

@trillllian

Copy link
Copy Markdown
Collaborator

feels kinda smelly that the IR0-15 shorthands only accept 6-digit hex. i think you should probably call into the math parser if the input starts with #. (if you need to disambiguate it from other immediate operands, check if expr->has_label() <= 1. if true, the expression should never change value between passes and you can use its value to choose the instruction to encode)

some of the smaller auxiliary frozen::unordered_maps are probably slower than a cascade of ifs, and more complicated. (in fact i think it's barely worth it even for the 65816 mnemonic lookup and the assembleblock lookup)

do sub/subr and cmp/cmpr need to be separate instructions? i assume the difference is which way around the subtraction happens, but in asm we can represent that with argument order. it looks like the only case where you can't tell which way around it's supposed to be is sub a, a, which seems like a somewhat pointless operation anyways; if you really need to express subr a,a you can always bang out the bytes with db manually.

somewhat shallow review bc i'm reading the code on my phone. i'm so overwhelmed with everything idk if i'll get around to doing a proper review in the next 6 months, but maybe after that

@trillllian

Copy link
Copy Markdown
Collaborator

also what's with the gitignore diff? iirc i spent quite a while getting dotnet to not spew its bullshit everywhere, is it still broken?

@Ersanio

Ersanio commented Nov 21, 2026 •

Copy link
Copy Markdown
Author

do sub/subr and cmp/cmpr need to be separate instructions? (...)

I think we can take the liberty of simplifying these four opcodes by combining SUB/SUBR and CMP/CMPR. Based on the argument order (A on the left or right), it'll be one or the other, and the user doesn't have to remember when they have to use xxxR; All they'll have to worry about is "left minus right".

SUB A << 8, #$42    ; machine code is SUB
SUB #$42, A << 8    ; machine code is SUBR
CMP A << 8, #$42    ; machine code is CMP
CMP #$42, A << 8    ; machine code is CMPR

I'm liking your idea, it'll remove a bit of complexity from Cx4 assembly. Considering there's no official documentation for this chip, I think we can get real creative with the remaining syntax as well to make Cx4 more accessible.

Regarding SUB A, A and SUBR A, A, one of the bsnes quirks is that when A is used as a register operand, it reads as the value $00 . In reality, this means two different things:

  • SUB A, A behaves like SUB A, #$00 (A is unchanged, processor flags affected)
  • SUBR A, A behaves like SUB #$00, A (negation)

This is also the case for other opcodes in bsnes (e.g. LD MDR, A loads 0 into MDR, not A)

The Mega Man X3 source never uses any form of A, A, so I don't think it was ever confirmed if A reading as the value $00 is either an emulation bug, or it's implemented as intended. Right now asar will warn the user if the usage of A means the value $00. If that indeed is true hardware behavior though, a SUB A, A should either:

  • error out and tell the user that this instruction is ambiguous and that one side should be replaced with IR0 (#$000000) instead
  • warn, continue assembling, truly default to SUB A, A in machine code

This quirk about A, A got me wondering if this is really the right behavior. I'm looking at how MesenCE handles Cx4 emulation, it seems to differ from bsnes in some cases:

  1. In MesenCE, using A as a register operand really does return A's value, whereas bsnes returns the value $00.
  2. In MesenCE, shift counts of 24 and higher result in no shift, but in bsnes, exactly 24 is still accepted, so a shift count of 24 produce different results in each
  3. in MesenCE, any register is accepted in LD P, reg, while bsnes only accepts LD P, R0 to R15

One document basically claims that:

  1. Inconclusive
  2. bsnes implements this correctly ("A count larger than twenty four (24) is replaced with zero (0)")
  3. bsnes implements this correcty ("01100011 ....xxxx 63 MOV P, Rx Data Transfer Instruction P = Rx")

And that's assuming this document is correct

It's unfortunate that I can't find any documentation regarding A, A's behavior. It's something I'd really like to test on real hardware. Considering that I want to test many more things on real hardware (a lot of things are marked "pending hardware confirmation"), I'm beginning to wonder if it's not a bit too early to create this PR, after all.

Maybe I'll close this PR and continue developing on my own asar cx4 fork until I feel it's ready again. What are your thoughts on this as a maintainer for asar?

also what's with the gitignore diff? iirc i spent quite a while getting dotnet to not spew its bullshit everywhere, is it still broken?

I don't remember when exactly I got the files there, it has a bunch of "interface-tests" related files:

  • src/tests/csharp-test/obj/interface-tests.csproj.nuget.dgspec.json
  • src/tests/csharp-test/obj/interface-tests.csproj.nuget.g.props
  • src/tests/csharp-test/obj/interface-tests.csproj.nuget.g.targets
  • src/tests/csharp-test/obj/project.assets.json
  • src/tests/csharp-test/obj/project.nuget.cache
  • src/tests/csharp-test/obj/Debug/net8.0/.NETCoreApp,Version=v8.0.AssemblyAttributes.cs
  • src/tests/csharp-test/obj/Debug/net8.0/interface-tests.AssemblyInfo.cs
  • src/tests/csharp-test/obj/Debug/net8.0/interface-tests.AssemblyInfoInputs.cache
  • src/tests/csharp-test/obj/Debug/net8.0/interface-tests.assets.cache
  • src/tests/csharp-test/obj/Debug/net8.0/interface-tests.GeneratedMSBuildEditorConfig.editorconfig
  • src/tests/csharp-test/obj/Release/net8.0/.NETCoreApp,Version=v8.0.AssemblyAttributes.cs
  • src/tests/csharp-test/obj/Release/net8.0/interface-tests.AssemblyInfo.cs
  • src/tests/csharp-test/obj/Release/net8.0/interface-tests.AssemblyInfoInputs.cache
  • src/tests/csharp-test/obj/Release/net8.0/interface-tests.GeneratedMSBuildEditorConfig.editorconfig

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants