Zig

From llvm-mos

The zig-mos project is based on llvm-mos and llvm-mos-sdk and allows Zig programs to run on 6502. If you want to use zig-mos, just get the available tarball (compatible with your operating system and computer architecture). If it's not available for your system, downloading bootstrap and running the build.(sh|bat) script on your local system to start building (with all dependencies already available) - no additional dependencies required!

Getting started[edit | edit source]

Once you've downloaded or built zig-mos, you can move on to the next step. It's also recommended to download llvm-mos-sdk, since the libraries and additional files aren't included in zig-mos.

Let's test zig-mos by downloading the zig-mos-examples project. Having downloaded and extracted all the contents of the zig-mos-examples project, you can then select which project to build and later test using a specified emulator or device.

Commands[edit | edit source]

Zig command line interface[edit | edit source]

  • 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[edit | edit source]

  • 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[edit | edit source]

$> 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[edit | edit source]

Similar to [Targets available](#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 C64DTVs 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 C64DTVs 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)[edit | edit source]

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.14.0-dev.mos.528+467e65f0c";
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;


Other resources[edit | edit source]