Skip to content

Instantly share code, notes, and snippets.

View petrihakkinen's full-sized avatar

Petri Häkkinen petrihakkinen

  • Almost Human Ltd. / Ctrl Alt Ninja Ltd.
  • Finland
  • X @petrih3
View GitHub Profile
// Multiplexed PWM RGB Led Demo
// By Petri Hakkinen
// 24th November 2014
//
// Arduino Uno driving a single RGB led with only one resistor.
//
// Make the following connections:
// * Connect Arduino pins D2,D3,D3 to anodes of a RGB led.
// * Connect cathode of the RGB led to 330 ohm resistor.
// * Connect the other end of the resistor to ground.
// stop CPU clock in high state
void stopClock() {
// wait for clock to go low
while((PIND & CPU_CLOCK) != 0) {}
// wait for clock to go high
// must check that WE is high, otherwise CPU may be writing to memory
while((PIND & (CPU_CLOCK|SRAM_WE)) == 0) {}
TCCR2B = 0; // stop timer by setting prescaler to 0
TCCR2A = 0; // disable timer so that we can control PD7 manually
}
; phase 1: 6502 is bus master (8 cycles)
; prepare atmega for memory access by preloading address (address lines are still inputs so this will only affect internal pull up resistors)
out PORTA, lo ; load lo address
out PORTB, hi ; load hi address
st X+, data ; store data received in previous phase 0
adiw address, 1 ; increment address
nop
nop
; phase 0: atmega is bus master (8 cycles)
; halt CPU (20 cycles)
sts TCCR2B, r0 ; TCCR2B = 0
call delay16 ; wait a while so that the 6502 can finish whatever it was doing
cbi _SFR_IO_ADDR(PORTD), BUS_ENABLE ; disable bus
; ATmega can now do whatever dirty tricks it likes in the memory
; restart CPU (5 cycles)
sbi _SFR_IO_ADDR(PORTD), BUS_ENABLE ; enable bus
ldi r24, 1
; copy 128 bytes, 6 cycles per byte (768 cycles)
.rept 128
out _SFR_IO_ADDR(PORTA), ZL ; PORTA = lo
addi ZL, 1
nop
in r24, _SFR_IO_ADDR(PINC) ; r24 = data
st X+, r24
.endr
const uint8_t rom[] = {
// clear screen
// screen memory starts at $c000 and is 50*25 = 1600 bytes long
// clear first 6*256 = 1536 bytes
0xa9, 32, // lda #32 'space'
0xa2, 0, // ldx #0
//clearloop:
0x9d, 0x00, 0xc0, // sta $c000,x
0x9d, 0x00, 0xc1, // sta $c100,x
0x9d, 0x00, 0xc2, // sta $c200,x
@petrihakkinen
petrihakkinen / st3_autcomplete_width_patch
Created April 27, 2015 13:25
Enlarge sublime text 3 autcomplete list width from 300 pixels to 600 pixels
-- sublime text 3 autcomplete dialog width is hardcoded to 300 pixels
-- this lua script changes it to 600 pixels by modifying the sublime text executable
-- backup the original executable before running the patch!
local filename = "c:/program files/sublime text 3/sublime_text.exe"
local hex = function(t)
local s = ""
for i=1,#t do
s = s..string.char(t[i])
@petrihakkinen
petrihakkinen / bresenham.asm
Last active June 11, 2024 05:05
Bresenham's line algorithm for 6502
;*****************************************************************
; negate accumulator
;*****************************************************************
neg: eor #$ff
clc
adc #1
rts
;*****************************************************************
@petrihakkinen
petrihakkinen / zx7.asm
Last active January 5, 2020 17:04
ZX7 decompressor for 6502 (150 bytes, 146 bytes if dest address is set by caller)
;*****************************************************************
; ZX7 decompressor based on
; ZX7 data decompressor for Apple II
; by Peter Ferrie (peter.ferrie@gmail.com)
; with modifications by Juan J. Martinez (@reidrac)
; optimized by Petri Häkkinen
; This code is in the Public Domain
;*****************************************************************
zx7_src_lo = $20 ; should always contain 0