Something went wrong. Try again.
A cycle-accurate Apple II and Apple IIgs emulator written in Zig, usable as both a program and a library.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>//// SPDX-License-Identifier: MIT
//! Addition and subtraction, binary and decimal, at eight bits or sixteen.//!//! Not `cpu/add.zig` generalised, and it is worth saying why, because that one//! is shared with this core: the clock and the 8-bit adder are the two things//! both cores use, precisely because neither knows what an address is. This is//! the exception. Decimal arithmetic here has to work over four nibbles as//! well as two, and widening the 8-bit adder to carry a nibble count would put//! a comptime parameter through the middle of code that twelve and a half//! million corpus cases currently pass, to serve a processor that does not//! share a single instruction with it. The rule the two cores were split under//! applies to this file too: duplicate rather than generalise, when the//! generalisation reaches into what already works.//!//! The corrections below are the CMOS ones — digit by digit going up, and by//! whole nibbles coming down — because that is what the 65816 inherited. What//! is new is doing them four nibbles at a time, and the SingleStepTests//! recordings are what settled that: half of every opcode's cases have the//! decimal flag set.
const std = @import("std");
/// What came out, and what the flags should be.pub fn Result(comptime T: type) type { return struct { value: T, C: bool, N: bool, Z: bool, V: bool, };}
/// How many nibbles wide the operation is: two at eight bits, four at sixteen.fn nibbles(comptime T: type) comptime_int { return @typeInfo(T).int.bits / 4;}
/// The sign bit at this width.fn sign(comptime T: type) T { return @as(T, 1) << (@typeInfo(T).int.bits - 1);}
/// Adds, in whichever base the decimal flag asks for.pub fn adc(comptime T: type, augend: T, addend: T, carry: bool, decimal: bool) Result(T) { if (decimal) return decimalAdd(T, augend, addend, carry);
const Wide = std.meta.Int(.unsigned, @typeInfo(T).int.bits + 1); const sum: Wide = @as(Wide, augend) + addend + @intFromBool(carry); const value: T = @truncate(sum); return .{ .value = value, .C = sum > std.math.maxInt(T), .N = value & sign(T) != 0, .Z = value == 0, // Overflow is about signs: two operands that agreed, and an answer // that does not agree with them. .V = ~(augend ^ addend) & (augend ^ value) & sign(T) != 0, };}
/// Subtracts, in whichever base the decimal flag asks for. The carry is a/// borrow the other way up, as it has been since the 6502: set means there is/// nothing to borrow.pub fn sbc(comptime T: type, minuend: T, subtrahend: T, carry: bool, decimal: bool) Result(T) { if (decimal) return decimalSubtract(T, minuend, subtrahend, carry);
const Wide = std.meta.Int(.signed, @typeInfo(T).int.bits + 2); const difference: Wide = @as(Wide, minuend) - subtrahend - @intFromBool(!carry); const value: T = @truncate(@as(std.meta.Int(.unsigned, @typeInfo(T).int.bits + 2), @bitCast(difference))); return .{ .value = value, .C = difference >= 0, .N = value & sign(T) != 0, .Z = value == 0, .V = (minuend ^ subtrahend) & (minuend ^ value) & sign(T) != 0, };}
/// Decimal addition, a digit at a time, each corrected as it is made.////// Correcting the whole number at once gives the same answer for digits that/// are really digits and the wrong one for the six values a nibble can hold/// that are not, which programs do use. Overflow is worked out from the top/// digit as it stands before it is corrected, which is why it means so little/// after a decimal add and is still worth reproducing exactly.fn decimalAdd(comptime T: type, augend: T, addend: T, carry: bool) Result(T) { const n = nibbles(T); var carry_in: u8 = @intFromBool(carry); var value: T = 0; var uncorrected_top: u8 = 0;
inline for (0..n) |i| { const shift = 4 * i; const a: u8 = @truncate((augend >> shift) & 0xf); const b: u8 = @truncate((addend >> shift) & 0xf); var digit: u8 = a + b + carry_in; if (i == n - 1) uncorrected_top = digit; if (digit > 9) digit += 6; carry_in = @intFromBool(digit > 0xf); value |= @as(T, digit & 0xf) << shift; }
const uncorrected: T = @truncate(@as(u32, uncorrected_top) << (4 * (n - 1))); return .{ .value = value, .C = carry_in != 0, .N = value & sign(T) != 0, .Z = value == 0, .V = ~(augend ^ addend) & (augend ^ uncorrected) & sign(T) != 0, };}
/// Decimal subtraction, a digit at a time.////// This is where the 65816 parts company with the 65C02, and the recordings/// are what said so. The older CMOS part corrects the whole number at once —/// six off if the low nibble borrowed, sixty more if the byte did — and that/// is what `cpu/add.zig` does, correctly, for twelve and a half million cases./// Do the same here and $b0 minus $4d minus a borrow comes out $5c; the/// hardware says $6c, which is what you get by taking each digit on its own/// and adding ten to it when it goes under. The two agree on every pair of/// digits that are really digits and disagree on the six values a nibble can/// hold that are not — and programs do use those.////// A digit that goes far enough under to still be negative after the ten is/// added keeps whatever four bits it has left, which is what the hardware's/// adder does and why the mask is where it is rather than a clamp.fn decimalSubtract(comptime T: type, minuend: T, subtrahend: T, carry: bool) Result(T) { const n = nibbles(T); const Wide = std.meta.Int(.signed, @typeInfo(T).int.bits + 8);
var borrow: Wide = @intFromBool(!carry); var value: T = 0; inline for (0..n) |i| { const shift = 4 * i; const a: Wide = @as(Wide, (minuend >> shift) & 0xf); const b: Wide = @as(Wide, (subtrahend >> shift) & 0xf); var digit: Wide = a - b - borrow; if (digit < 0) { digit += 10; borrow = 1; } else borrow = 0; value |= @as(T, @intCast(digit & 0xf)) << shift; }
// Overflow still comes from the binary difference, as it does after a // decimal add: it is about signs, and signs are a binary idea. const binary: Wide = @as(Wide, minuend) - subtrahend - @intFromBool(!carry); const Unsigned = std.meta.Int(.unsigned, @typeInfo(T).int.bits + 8); const wrapped: T = @truncate(@as(Unsigned, @bitCast(binary))); return .{ .value = value, .C = borrow == 0, .N = value & sign(T) != 0, .Z = value == 0, .V = (minuend ^ subtrahend) & (minuend ^ wrapped) & sign(T) != 0, };}
test "binary addition at both widths" { try std.testing.expectEqual(@as(u8, 0x02), adc(u8, 1, 1, false, false).value); try std.testing.expectEqual(@as(u16, 0x0002), adc(u16, 1, 1, false, false).value); // The carry out is at the top of whichever width is in force, which is the // whole reason the width has to reach the adder at all. try std.testing.expect(adc(u8, 0xff, 0x01, false, false).C); try std.testing.expect(!adc(u16, 0x00ff, 0x0001, false, false).C); try std.testing.expect(adc(u16, 0xffff, 0x0001, false, false).C);}
test "decimal addition carries between digits, and between bytes" { try std.testing.expectEqual(@as(u8, 0x20), adc(u8, 0x19, 0x01, false, true).value); try std.testing.expectEqual(@as(u16, 0x0020), adc(u16, 0x0019, 0x0001, false, true).value); // Four digits, so the carry out of the second digit goes on into the third // rather than out of the register. try std.testing.expectEqual(@as(u16, 0x1000), adc(u16, 0x0999, 0x0001, false, true).value); try std.testing.expect(!adc(u16, 0x0999, 0x0001, false, true).C); try std.testing.expect(adc(u16, 0x9999, 0x0001, false, true).C);}
test "decimal subtraction borrows the same way" { try std.testing.expectEqual(@as(u8, 0x29), sbc(u8, 0x30, 0x01, true, true).value); try std.testing.expectEqual(@as(u16, 0x0029), sbc(u16, 0x0030, 0x0001, true, true).value); try std.testing.expectEqual(@as(u16, 0x0999), sbc(u16, 0x1000, 0x0001, true, true).value); try std.testing.expect(sbc(u16, 0x1000, 0x0001, true, true).C); try std.testing.expect(!sbc(u16, 0x0000, 0x0001, true, true).C);}