Skip to content

Instantly share code, notes, and snippets.

@raxoft
raxoft / easter.rb
Created March 19, 2024 13:56
Ruby code to compute the date of Easter Sunday for given year.
# Helper for computing the date of Easter Sunday.
require 'date'
class Date
def self.easter_sunday(year)
a = year % 19
b, c = year.divmod 100
d, e = b.divmod 4
g = (8 * b + 13) / 25
@raxoft
raxoft / mouse.asm
Last active March 4, 2020 22:20
Kempston mouse driver for ZX Spectrum.
; Kempston mouse driver with master/slave support.
; Written by Patrik Rak in 2013, and revised in 2016.
; Based on a driver originally written for testing ZXDS, see the original WoS post
; in the thread at http://www.worldofspectrum.org/forums/discussion/43026/mouse-systems-for-zx-spectrum
updatemouse
ld hl,ports
ld de,mouseinput
ld c,0xDF
.loop ld b,(hl)
@raxoft
raxoft / sudoku.rb
Last active June 14, 2022 13:56
Fast and compact Sudoku solver. I wrote it after seeing the example in "The Ruby Programming Book" to find out how much simpler and cleaner it could be made. I have improved it sligthly later and added a generator just for fun.
# Compact Sudoku solver and generator
#
# Written by Patrik Rak in 2009 to test the power of Ruby
class Sudoku
# Dimensions.
B = 3
N = B * B
SIZE = N * N
@raxoft
raxoft / z80d.cpp
Created May 27, 2015 15:53
Disassembler of the Z80 instruction set. I wrote it for ZXDS, but made it public later after I have found that none of the publicly available code really reaches the bar, especially as far as the IX/IY instructions are concerned.
// Z80 disassembler.
//
// Written by Patrik Rak for ZXDS in 2010, released under MIT license in 2011.
//
// There are two main entry points, one for doing the full blown disassembly,
// the other one for just measuring how many bytes given opcode sequence takes.
//
// The full details are below:
//
// const char *
@raxoft
raxoft / money.rb
Last active August 29, 2015 14:21
Simple Money helper. Showcases how to integrate custom types into Ruby, coerce them into other types and define binary operators properly. Example of an extensive unit test is included as well.
# Simple Money helper.
#
# Written by Patrik Rak in 2014.
# Money class for convenient working with amounts internally stored in cents.
class Money
# Helper class for handling left hand side arguments of binary operators.
class Scalar
include Comparable
@raxoft
raxoft / cmwc.asm
Last active August 2, 2022 14:29
Fast quality CMWC random number generator for Z80 I have created to weed out the crap RNGs out there. 32+10 bytes, 146..149 T cycles, period 253*2^59, which is more than 2^66 or 10^20.
; 8-bit Complementary-Multiply-With-Carry (CMWC) random number generator.
; Created by Patrik Rak in 2012, and revised in 2014/2015,
; with optimization contribution from Einar Saukas and Alan Albrecht.
; See http://www.worldofspectrum.org/forums/showthread.php?t=39632
org 40000
call rnd ; BASIC driver
ld c,a
ld b,0
@raxoft
raxoft / xorshift.asm
Created May 22, 2015 07:36
Fast quality Xor-Shift random number generator for Z80 I have created to weed out the crap RNGs out there. 28 bytes, 122 T cycles, period 2^32-1.
; 8-bit Xor-Shift random number generator.
; Created by Patrik Rak in 2008 and revised in 2011/2012.
; See http://www.worldofspectrum.org/forums/showthread.php?t=23070
org 40000
call rnd ; BASIC driver
ld c,a
ld b,0
ret
@raxoft
raxoft / mufs_password_decoder.rb
Last active July 4, 2024 11:19
This is the decoder of the password format used by the Amiga MultiUser filesytem. I wrote it the other day when I needed to login to an old A4000 whose root password I forgot. Note that some letters in the passwords are ambiguous due to modulo 53 being used, so 'A' and 'v' are the same, as is '0' and 'e' or space and 'U', so take your pick from …
# Decoder for passwords created by multiuser.library of Amiga MultiUser muFS filesystem (ACrypt() encoded, AKA AS225r2 format).
# Copyright (C) 2015 Patrik Rak ([email protected])
# This source code is released under the MIT license.
def decode( password, user, base = 'A' )
r = 53
s = password.bytes.map{ |x| x - 'A'.ord } + [ 0 ] * 12