From 51fdaab28da64239ab9e4ce09c9f6eb17742d258 Mon Sep 17 00:00:00 2001 From: giomba Date: Thu, 3 Nov 2022 20:51:18 +0100 Subject: [PATCH] Add bouncing sprite. First experiment. --- const.h | 2 +- main.S | 49 ++++++++++---------------------------- main.c | 39 +++++++++++++++++++++++++----- samples/bouncing-logo.pbm | 40 +++++++++++++++++++++++++++++++ samples/bouncing-logo.xcf | Bin 0 -> 2871 bytes 5 files changed, 87 insertions(+), 43 deletions(-) create mode 100644 samples/bouncing-logo.pbm create mode 100644 samples/bouncing-logo.xcf diff --git a/const.h b/const.h index 51c8eb5..382873f 100644 --- a/const.h +++ b/const.h @@ -1,7 +1,7 @@ #pragma once #define VERTICAL_OFFSET 30 -#define HORIZONTAL_OFFSET 192 +#define HORIZONTAL_OFFSET 168 #define LINE_BUFFER_SIZE 160 #define BLACK 0x00 diff --git a/main.S b/main.S index 15e2fd5..e6b5978 100644 --- a/main.S +++ b/main.S @@ -36,29 +36,14 @@ main_asm: 1: rjmp 1b -.global int_horizontal_sync -int_horizontal_sync: ; +3 - push r31 ; +5 - in r31, IO(SREG) ; +6, status register - push r31 ; +8 - push r30 ; +10 +.global int_horizontal_sync_s +int_horizontal_sync_s: + push r31 - ; if (0 <= line < 256), then enter - lds r30, line ; +12 - lds r31, line + 1 ; +14 - adiw z, 1 ; +16 - sts line, r30 ; +18 - sts line + 1, r31 ; +20 - cpi r31, 0 ; +21 - breq enter ; +23 - jmp int_horizontal_sync_end - -enter: - ; here, +23 cycles have passed since horizontal sync - ; so, there are still ~168 cycles before first useful data + ; load timer to trigger isr at the beginning of the visible scanline ldi r31, 0 sts TCNT0, r31 - ldi r31, HORIZONTAL_OFFSET ; set counter TOP + lds r31, hpos ; set counter TOP sts OCR0A, r31 ldi r31, 0x7 ; clear any pending interrupt @@ -68,12 +53,8 @@ enter: ori r31, 0x02 ; mask enable interrupt timer A sts TIMSK0, r31 -int_horizontal_sync_end: - pop r30 pop r31 - out IO(SREG), r31 - pop r31 - reti + ret .global int_timer_0 int_timer_0: @@ -83,6 +64,7 @@ int_timer_0: push r31 push r30 push r29 + push r25 ; turn off interrupt lds r31, TIMSK0 @@ -96,11 +78,11 @@ int_timer_0: lds zl, current_jump_table lds zh, current_jump_table + 1 -; ldi zl, pm_lo8(line_jump_table_0) -; ldi zh, pm_hi8(line_jump_table_0) - clr r0 lds r29, line + lds r25, vpos + sub r29, r25 + add zl, r29 adc zh, r0 add zl, r29 @@ -110,6 +92,7 @@ int_timer_0: .global jump_table_return_address jump_table_return_address: + pop r25 pop r29 pop r30 pop r31 @@ -117,8 +100,8 @@ jump_table_return_address: pop r31 reti -.global int_vertical_sync -int_vertical_sync: +.global int_vertical_sync_s +int_vertical_sync_s: push r31 in r31, IO(SREG) push zl @@ -126,12 +109,6 @@ int_vertical_sync: push yl push yh - lds r31, frame + 1 - lds r30, frame - adiw z, 1 - sts frame + 1, r31 - sts frame, r30 - ldi r30, 1 ldi r31, 0 sts line, r30 diff --git a/main.c b/main.c index c3ac480..a07a29e 100644 --- a/main.c +++ b/main.c @@ -18,14 +18,41 @@ volatile uint16_t frame = 0; volatile uint16_t line = 0xfe; volatile uint8_t image = 0xff; -ISR(INT0_vect, ISR_NAKED) { - // vertical sync interrupt - asm("jmp int_vertical_sync"); +// horizontal and vertical position of bouncing logo +volatile uint8_t vpos = 0; +volatile uint8_t hpos = 0; +volatile int8_t vinc = +1; +volatile int8_t hinc = +1; + +void int_vertical_sync_s(void); +ISR(INT0_vect) { + frame += 1; + vpos += vinc; + hpos += hinc; + + if (vpos == 0) + vinc = +1; + if (vpos == 256 - 64) + vinc = -1; + if (hpos == HORIZONTAL_OFFSET) + hinc = +1; + if (hpos == 255 - 40) + hinc = -1; + + int_vertical_sync_s(); } -ISR(INT1_vect, ISR_NAKED) { - // horizontal sync interrupt - asm("jmp int_horizontal_sync"); + +void int_horizontal_sync_s(void); +ISR(INT1_vect) { + line += 1; + // if (line >= 256) + if (line >= vpos + 64) + return; + + if (line >= vpos) + int_horizontal_sync_s(); } + ISR(TIMER0_COMPA_vect, ISR_NAKED) { // back porch timer interrupt asm("jmp int_timer_0"); diff --git a/samples/bouncing-logo.pbm b/samples/bouncing-logo.pbm new file mode 100644 index 0000000..126dd94 --- /dev/null +++ b/samples/bouncing-logo.pbm @@ -0,0 +1,40 @@ +P1 +# Created by GIMP version 2.10.32 PNM plug-in +40 64 +1110011101110111000110000011111111111111100101000010010010100100000000 +0000000001100101000010010010100100000000000000000111100110001001110010 +0100000000000000000110010100001001001010010000000000000000011001010000 +1001001010010000000000000000011001010000100100101001000000000000000001 +1001011100100100100110000000000000000001000000000000000000000000000000 +0000000001000000000000000000000000000000000000000100000000000000000000 +0000000000000000000100000000000000000000000000000000000000010000000000 +0000000000000000000000000000010000000000000000000000000000000000000001 +0000000000000000000000000000000000000001000000000000000000000000000000 +0000000001100000000000000000000000000000000000000110000000000000000000 +0000000000000000000110000000000000000000000000000000000000011000000000 +0000000000000000000000000000011000000000000000000000000000000000000001 +1000000000000000000000000000000000000001100000000000000000000000000000 +0000000001100000000000000000000000000000000000000110000000000000000000 +0000000000000000000110000000000000000000000000000000000000001000000000 +0000000000000000000000000000001000000000000000000000000000000000000000 +1000000000000000000000000000000000000000100000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000010000000000000000000000000000000000000001000000000 +0000000000000000000000000000001000000000000000000000000000000000000000 +1000000000000000000000000000000000000001100000000000000000000000000000 +0000000001100000000000000000000000000000000000000110000000000000000000 +0000000000000000000110000000000000000000000000000000000000011000000000 +0000000000000000000000000000011000000000000000000000000000000000000001 +1111110000000000000000000000000000011111 \ No newline at end of file diff --git a/samples/bouncing-logo.xcf b/samples/bouncing-logo.xcf new file mode 100644 index 0000000000000000000000000000000000000000..1054ca8fbe8f8c5959ceea7fe3922b31860ae307 GIT binary patch literal 2871 zcmcguU1%It6h5;%Bu(0wjYgvo;dc9D`mk{~smVsRX>F29D77JxBAAD4c4s%Eo1Hj2 zu^S(ZF9D&i8$~R{g7_eUqEst{)(6oC!Ka9z2tvC)7GjcaZ8KTFb7wY8mu?n(@a~*D z-#y>C=YIFhok`OA?3~CaQsT{UBtmFiuu}}s-h;Kl6b0R5uzf=x5e;{q0;G1B3iHFB za52X{4%>jWq>(z5FlJ}9tc5W#`RI_T#Vsu3nW3p3t*t5eZ|o z@W99&gyyrlCG>1!M$7RGBbCZ&mXPrt?7#$3J!X3rw2ECu<+ph9(=%qTRgZGnAhXR zA{$}69_K&eV%+F3<1!lVhdkJOHgF!4@4z_^+0yb>C=*}MWPOlZ`M7H~ZoWQ0CxS5t z494&@vLa<{-q{SbcMs4dRLOq9Qn9%$o^K3>isHjG2ZwZDf)aHpJ;g&v7?c4xN)Qr{}c{ADejA z(&HIDo)Zi57-izw^gPbC2Z;uJg0?{x~U~B-N=fJWx`o~xSg}jNB6b)oTaB0 zL;|mp#-Ye`-AMJ^{ygd86o0!qD3%o3H3wV6qrk?bSDu>xUkLs%r;owqp75`$>S*qAk zQUV*k)EeD@&h+z*jZ=v-3gHHe%lCm|9&>r{Qv zsVplHiJo;5VXc3VnOd+u-MO{)v?CMl`vsCR?<~b!nU2$uxjS`*-r%Y0>>zsnml!0X zmnxx;pwGF6>3$Qw4NV8>9u{o=>42bnUsGXmbqV0_^M&ipHvm4r{vlX^!rOoS@%^6+ z0>6Jb_7wvvI23k`Yf)X}hvxm0&^UotBx$hQ#~oU|4;Ftw{?88dv7k<>+P#WEe;(-b-aY4xl+$D;p6Ro2=W8mP0V;-o1h6w)V2myRvIc=dg7nE?6w=RELT6tXI7?F=ZSt