Skip to content

Instantly share code, notes, and snippets.

View shirriff's full-sized avatar

Ken Shirriff shirriff

View GitHub Profile
@shirriff
shirriff / notes.py
Last active December 23, 2021 21:13
UM66T sound chip analysis program
# This code prints out the melody in the UM66T sound chip's ROM.
# The ROM holds Jingle Bells, Santa Claus is Coming to Town, and We Wish You a Merry Christmas.
# See this Twitter thread for details: https://twitter.com/kenshirriff/status/1472297415201869831
from collections import defaultdict
notes = ['X', 'X', 'G4', 'A5', 'B5', 'C5', 'D5', 'E5', 'F5', 'G5', 'A6', 'B6', 'C6', 'X', 'X', 'X']
# Hard-coded ROM contents.
bits = {"24,1":0,"15,0":1,"15,1":0,"15,3":1,"15,4":0,"14,4":1,"13,4":0,"12,4":1,"11,4":0,"10,4":1,"9,4":1,"8,4":0,"7,4":0,"6,4":1,"5,4":1,"4,4":0,"0,4":0,"1,4":1,"2,4":1,"3,4":0,"0,1":1,"0,0":1,"3,0":1,"4,0":1,"7,0":1,"8,0":1,"11,0":1,"12,0":1,"17,0":1,"19,0":1,"20,0":1,"23,0":1,"25,0":1,"26,0":1,"29,0":1,"31,0":1,"30,1":1,"28,1":1,"26,1":1,"25,1":1,"22,1":1,"21,1":1,"18,1":1,"17,1":1,"30,5":1,"31,6":1,"30,8":1,"30,9":1,"30,10":1,"31,11":1,"31,13":1,"31,14":1,"30,15":1,"31,16":1,"30,18":1,"30,19":1,"31,20":1,"31,21":1,"30,23":1,"31,24":1,"31,25":1,"31,26":1,"31,28":1,"30,29":1,"30,28":0,"29,
@shirriff
shirriff / days-in-month
Last active October 24, 2022 14:22
HP Nanoprocessor code to determine the number of days in a month
d0 STR-0 Store the next byte (7) in register 0.
07
0c SLE Skip two instructions if accumulator <= register 0.
03 DED Decrement the accumulator in decimal mode
5f NOP No operation
d0 STR-0 Store the next byte (0x31) in register 0
31
30 SBZ-0 Skip two instruction bytes if accumulator bit 0 is zero
81 JMP-1 Jump to 0x1c9 (end of this code block)
c9
@shirriff
shirriff / tan.py
Created May 14, 2020 01:04
Demo of the 8087's CORDIC tangent algorithm
import math
# 8087 tangent algorithm, from Implementing Transcendental Functions, R. Nave
atan_table = [math.atan(2 ** -i) for i in range(0, 17)]
def cordic_tan(z):
# Compute tan of angle z, using CORDIC
q = []
# Break down z into sum of angles, where each angle is 0 or atan(2**-i)
@shirriff
shirriff / music_1401.txt
Created September 7, 2019 21:24
Music program for the IBM 1401, along with frequency cards. Courtesy of Ron Mak.
,008015,022033L067135,1050291001/099H104C104135B101/,0011/001199199 0001MUSIC
,109116,121125,126133/101099 0002MUSIC
,008015,022029,036040,047054,061068,072/061039 ,00100110400003MUSIC
0000000000000,001/000H099L/49089 L032118,092097,100104,10811210400004MUSIC
L/490941C003/53B334SM0996J5B165DL/54354L039157,126127,134139,14615110400005MUSIC
M0756J1A/56094M0660?0H0991L0660?0H099 L037194,165172,179183,18419110400006MUSIC
A/57089C089/59B126U.100 L023217,202209,214040,04004010400007MUSIC
M585099/332/,0052011N372M002005 L031364,341345,346353,35435810400008MUSIC
M/60002B564001XB353001 B503001RL/49089 L038402,372380,388396,04004010400009MUSIC
C0036/1B438SA/56089C089094B403U.100 L035437,410415,422429,43404010400010MUSIC
@shirriff
shirriff / music-1403.txt
Created September 2, 2019 16:21
Disassembly of a vintage program to generate music on an IBM 1403 line printer.
87 00000 # X0
92 00000 # X1
97 000 # X2
100 ,001 set wordmark 1 # Start
104 /000 clear storage 0 # Wraps to top of memory?
108 H099 store B* 99 # Top of memory - 100? into X2
112 L/49089 load 1149 89 # X1 = 0
119 L/49094 load 1149 94 # X2 = 0
126 1 read # READNOTE: Read first card
127 C003/53 compare 3 1153 # Card has END?
@shirriff
shirriff / prime1.s
Created June 21, 2018 05:06
Generate primes up to 255 on the IBM 1401
job prime1.s
* Compute primes 2-255 for toggle challenge
* Ken Shirriff http://righto.com
ctl 6641
org 201 * Start of print buffer
num dcw 002 * Current number
org 333 * Start of code memory
outer mcw @002@, factor * Start with factor 2
@shirriff
shirriff / fpga-font.py
Created April 4, 2018 03:36
Generate FPGA code to implement a character set
# Process font file to generate FPGA code
# Font from https://github.com/dhepper/font8x8/blob/master/font8x8_basic.h
import re
chars = []
for line in open('font8x8_basic.h').readlines():
m = re.search('{([\s0-9A-Fa-fx,]*)}', line)
if m:
@shirriff
shirriff / fpga-font.py
Created April 4, 2018 03:36
Generate FPGA code to implement a character set
# Process font file to generate FPGA code
# Font from https://github.com/dhepper/font8x8/blob/master/font8x8_basic.h
import re
chars = []
for line in open('font8x8_basic.h').readlines():
m = re.search('{([\s0-9A-Fa-fx,]*)}', line)
if m:
@shirriff
shirriff / edid.py
Created March 25, 2018 15:58
Parse VGA configuration data (EDID) accessed from I2C device 0x50
# Program to parse VGA data (EDID) accessed from I2C device 0x50
#
# This is a quick demo not a supported program, so don't expect
# correctness from it.
#
# Edid format from:
# https://en.wikipedia.org/wiki/Extended_Display_Identification_Data#EDID_1.4_data_format
#
# Ken Shirriff http://righto.com
@shirriff
shirriff / PRU code
Last active January 8, 2018 21:33
Fragment of PRU code to output Manchester-encoded data.
for (bit_count = 0; bit_count < 8; bit_count++) {
if (byte & 0x80) {
wait_for_pwm_timer();
__R30 = HIGH << WRITE_PIN;
wait_for_pwm_timer();
__R30 = LOW << WRITE_PIN;
} else {
wait_for_pwm_timer();
__R30 = LOW << WRITE_PIN;
wait_for_pwm_timer();