Skip to content

Instantly share code, notes, and snippets.

@kassane
Last active May 4, 2024 17:46
Show Gist options
  • Save kassane/4e8d485a09aaab475aa17f6ca3818fcb to your computer and use it in GitHub Desktop.
Save kassane/4e8d485a09aaab475aa17f6ca3818fcb to your computer and use it in GitHub Desktop.
zig-mos

Zig on MOS6502 (nes, c64, atari8, atari2600)

Zig toolchain [forked] uses LLVM codegen for mos target.

Current version:

Commands

Zig command line interface:

  • build-lib: build static-lib or shared-lib (add -dynamic flag);
  • build-obj: build object file, like clang/gcc -c.
  • build-exe: build executable
  • build: build-system mode, need build.zig.

Clang command line interface:

  • zig cc: clang CLI
  • zig c++: clang++ CLI (uses llvm-libc++ + llvm-libunwind by default)

Note: Zig toolchain does not change libclang codegen. However, the default config enable -fsanitize=undefined.

Targets available

$> zig build-obj --show-builtin -target mos-freestanding -mcpu=
info: available CPUs for architecture 'mos':
 mos4510
 mos45gs02
 mos6502
 mos6502x
 mos65c02
 mos65ce02
 mos65dtv02
 mos65el02
 moshuc6280
 mosr65c02
 mosspc700
 mossweet16
 mosw65816
 mosw65c02

error: unknown CPU: ''

Note: Freestanding targets are not listed on zig targets | jq .libc (triple-targets)

CPU Features

Similar to Targets available command, add -mcpu or -Dcpu= (need build.zig).

  • + add feature
  • - remove feature

Note: For show feature list add +/- without feature name

e.g.:

$> zig build-obj --show-builtin -target mos-freestanding -mcpu=mos6502+
info: available CPU features for architecture 'mos':
 mos4510: CSG 4510
 mos45gs02: 45GS02
 mos6502: Generic MOS 6502, with support for BCD instructions
 mos6502x: NMOS 6502, with illegal opcode support
 mos65c02: Generic MOS 65C02
 mos65ce02: Commodore 65CE02
 mos65dtv02: The C64DTV's 6502 variant
 mos65el02: 65EL02 virtual machine
 mos_insns_4510: The new instructions present on CSG 4510
 mos_insns_45gs02: The new instructions present on 45GS02
 mos_insns_6502: The original documented 6502 instruction set
 mos_insns_6502bcd: BCD instruction support, including SED and CLD (most 6502 series CPUs support this)
 mos_insns_6502x: The 'illegal' opcodes present on some early variants of the original 6502 processor
 mos_insns_65c02: The new and modified instructions present on the generic 65c02 and variants
 mos_insns_65ce02: The new and modified instructions present on 65ce02 and variants
 mos_insns_65dtv02: The new and modified instructions present on the C64DTV's 6502 variant
 mos_insns_65el02: The new and modified instructions present on 65EL02
 mos_insns_huc6280: The new and modified instructions present on HuC6280
 mos_insns_r65c02: The new and modified instructions present on Rockwell and WDC 65c02
 mos_insns_spc700: The SPC700 instruction set
 mos_insns_sweet16: The SWEET16 instruction set
 mos_insns_w65816: The new and modified instructions present on WDC 65816
 mos_insns_w65c02: The new and modified instructions present on WDC 65c02
 mos_long_register_names: Requires llvm_mos_* prefixes to all registers. Useful if your code has variable names that conflict with llvm-mos register names
 moshuc6280: Hudson Soft HuC6280
 mosr65c02: Rockwell 65C02
 mosspc700: Sony 6502-like CPUs, including the SPC700
 mossweet16: MOS 6502 compatible with SWEET16 virtual machine support
 mosw65816: WDC 65816
 mosw65c02: WDC 65C02
 static_stack: Whether to use statically-allocated stack frames if possible.

error: unknown CPU feature: ''

Target info (builtin)

Note: If like syntax-highlighting use | bat -p -l zig pipeline command or save this output as builtin.zig and open on your code editor.

$> zig build-obj --show-builtin -target mos-freestanding
const std = @import("std");
/// Zig version. When writing code that supports multiple versions of Zig, prefer
/// feature detection (i.e. with `@hasDecl` or `@hasField`) over version checks.
pub const zig_version = std.SemanticVersion.parse(zig_version_string) catch unreachable;
pub const zig_version_string = "0.13.0-dev.mos.28+5zj4gh46";
pub const zig_backend = std.builtin.CompilerBackend.stage2_llvm;

pub const output_mode = std.builtin.OutputMode.Obj;
pub const link_mode = std.builtin.LinkMode.static;
pub const is_test = false;
pub const single_threaded = false;
pub const abi = std.Target.Abi.eabi;
pub const cpu: std.Target.Cpu = .{
    .arch = .mos,
    .model = &std.Target.mos.cpu.mos6502,
    .features = std.Target.mos.featureSet(&[_]std.Target.mos.Feature{
        .mos6502,
        .mos_insns_6502,
        .mos_insns_6502bcd,
        .static_stack,
    }),
};
pub const os = std.Target.Os{
    .tag = .freestanding,
    .version_range = .{ .none = {} },
};
pub const target: std.Target = .{
    .cpu = cpu,
    .os = os,
    .abi = abi,
    .ofmt = object_format,
    .dynamic_linker = std.Target.DynamicLinker.none,
};
pub const object_format = std.Target.ObjectFormat.elf;
pub const mode = std.builtin.OptimizeMode.Debug;
pub const link_libc = false;
pub const link_libcpp = false;
pub const have_error_return_tracing = true;
pub const valgrind_support = false;
pub const sanitize_thread = false;
pub const position_independent_code = false;
pub const position_independent_executable = false;
pub const strip_debug_info = false;
pub const code_model = std.builtin.CodeModel.default;
pub const omit_frame_pointer = false;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment