From a5b5fb17c6dd5ad5dc187d7c0d5163374ef06a9a Mon Sep 17 00:00:00 2001 From: giomba Date: Fri, 4 Nov 2022 20:51:43 +0100 Subject: [PATCH] Code finalization for bouncing sprite. --- const.h | 17 +++++--- main.S | 87 +++++++++++++++++++++----------------- main.c | 32 ++++++++------ samples/bouncing-logo.pbm | 87 +++++++++++++++++++++++++++----------- samples/bouncing-logo.xcf | Bin 2871 -> 4029 bytes 5 files changed, 141 insertions(+), 82 deletions(-) diff --git a/const.h b/const.h index 382873f..1bd619f 100644 --- a/const.h +++ b/const.h @@ -1,8 +1,15 @@ #pragma once -#define VERTICAL_OFFSET 30 -#define HORIZONTAL_OFFSET 168 -#define LINE_BUFFER_SIZE 160 +#define FRAME_PIXEL_WIDTH 1024 // [system clock] +#define FRAME_PIXEL_HEIGHT 256 // [line] -#define BLACK 0x00 -#define WHITE 0xff +#define BACK_PORCH_OFFSET 100 // [system clock] +#define FRONT_PORCH_OFFSET 40 // [system clock] + +#define SPRITE_WIDTH 80 // [system clock] +#define SPRITE_HEIGHT 64 // [line] + +// Time spent from the horizontal sync pulse, +// to the first displayed dot. +// This is given as HSYNC_INT_TUNE + HSYNC_TIMER_TUNE + C part +#define HSYNC_TUNE 70 \ No newline at end of file diff --git a/main.S b/main.S index cf5a9a0..7b04901 100644 --- a/main.S +++ b/main.S @@ -38,67 +38,76 @@ main_asm: .global int_horizontal_sync_s int_horizontal_sync_s: - push r31 - push r30 + push r31 ; +2 + push r30 ; +2 ; load timer to trigger isr at the beginning of the visible scanline ; reset counter to 0 - ldi r31, 0 - sts TCNT1H, r31 - sts TCNT1L, r31 + ldi r31, 0 ; +1 + sts TCNT1H, r31 ; +2 + sts TCNT1L, r31 ; +2 ; set counter TOP (MSB first!) - lds r31, hpos + 1 - lds r30, hpos - sts OCR1AH, r31 - sts OCR1AL, r30 + lds r31, hpos + 1 ; +2 + lds r30, hpos ; +2 + sts OCR1AH, r31 ; +2 + sts OCR1AL, r30 ; +2 ; clear any pending interrupt - ldi r31, 0x7 - sts TIFR1, r31 + ldi r31, 0x7 ; +1 + sts TIFR1, r31 ; +2 ; enable interrupt timer A - lds r31, TIMSK1 - ori r31, 0x02 - sts TIMSK1, r31 + lds r31, TIMSK1 ; +2 + ori r31, 0x02 ; +1 + sts TIMSK1, r31 ; +2 + + ; total: 25 + ; HSYNC_INT_TUNE = 25 + ; this adds up for HSYNC_TUNE pop r30 pop r31 ret -.global int_timer_0 -int_timer_0: +.global int_timer_1 +int_timer_1: ; here we are at the beginning of the visible line - push r31 - in r31, IO(SREG) - push r31 - push r30 - push r29 - push r25 + ; jmp (from C) ; +3 + push r31 ; +2 + in r31, IO(SREG) ; +1 + push r31 ; +2 + push r30 ; +2 + push r29 ; +2 + push r25 ; +2 ; turn off interrupt - lds r31, TIMSK1 - andi r31, 0xfd ; mask disable interrupt timer A - sts TIMSK1, r31 + lds r31, TIMSK1 ; +2 + andi r31, 0xfd ; mask disable interrupt timer A, +1 + sts TIMSK1, r31 ; +2 - lds r31, show_image - cpi r31, 0x1 - brne jump_table_return_address + lds r31, show_image ; +2 + cpi r31, 0x1 ; +1 + brne jump_table_return_address ; +1 (not taken) - lds zl, current_jump_table - lds zh, current_jump_table + 1 + lds zl, current_jump_table ; +2 + lds zh, current_jump_table + 1 ; +2 - clr r0 - lds r29, line - lds r25, vpos - sub r29, r25 + clr r0 ; +1 + lds r29, line ; +2 + lds r25, vpos ; +2 + sub r29, r25 ; +1 - add zl, r29 - adc zh, r0 - add zl, r29 - adc zh, r0 + add zl, r29 ; +1 + adc zh, r0 ; +1 + add zl, r29 ; +1 + adc zh, r0 ; +1 - ijmp + ijmp ; +2 + + ; total: 39 + ; HSYNC_TIMER_TUNE = 39 + ; this adds up for HSYNC_TUNE .global jump_table_return_address jump_table_return_address: diff --git a/main.c b/main.c index f430d8c..ef3f934 100644 --- a/main.c +++ b/main.c @@ -18,35 +18,46 @@ volatile uint16_t frame = 0; volatile uint16_t line = 0xfe; volatile uint8_t image = 0xff; -// horizontal and vertical position of bouncing logo +// horizontal and vertical position +// (and current increments) +// of bouncing sprite volatile uint8_t vpos = 0; volatile uint16_t hpos = 0; volatile int8_t vinc = +1; volatile int8_t hinc = +1; +// vertical sync interrupt void int_vertical_sync_s(void); ISR(INT0_vect) { + // count frames frame += 1; + + // move sprite vpos += vinc; hpos += hinc; + // invert bouncing direction if (vpos == 0) vinc = +1; - if (vpos == 256 - 64) + if (vpos == FRAME_PIXEL_HEIGHT - SPRITE_HEIGHT) vinc = -1; - if (hpos == HORIZONTAL_OFFSET) + if (hpos == BACK_PORCH_OFFSET) hinc = +1; - if (hpos == 1024 - 80) + if (hpos == + FRAME_PIXEL_WIDTH - SPRITE_WIDTH - FRONT_PORCH_OFFSET - HSYNC_TUNE) hinc = -1; int_vertical_sync_s(); } +// horizontal sync interrupt void int_horizontal_sync_s(void); ISR(INT1_vect) { + // count lines line += 1; - // if (line >= 256) - if (line >= vpos + 64) + + // we are past the last line of the sprite + if (line >= vpos + SPRITE_HEIGHT) return; if (line >= vpos) @@ -54,8 +65,8 @@ ISR(INT1_vect) { } ISR(TIMER1_COMPA_vect, ISR_NAKED) { - // back porch timer interrupt - asm("jmp int_timer_0"); + // end-of back porch timer interrupt + asm("jmp int_timer_1"); } // external assembly main loop @@ -76,10 +87,5 @@ int main() { TCCR1A = 0x00; // don't connect output pins to timer, CTC[1:0] mode TCCR1B = 0x09; // CTC[3:2] mode, no prescaler -#if 0 - TCCR0A = 0x02; // don't connect output pins to timer, CTC[1:0] mode - TCCR0B = 0x01; // CTC[2] mode, no prescaler -#endif - main_asm(); } diff --git a/samples/bouncing-logo.pbm b/samples/bouncing-logo.pbm index 126dd94..442fc7f 100644 --- a/samples/bouncing-logo.pbm +++ b/samples/bouncing-logo.pbm @@ -1,23 +1,6 @@ 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 +80 64 0000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000 @@ -31,10 +14,64 @@ P1 0000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000 -0000000000000000000010000000000000000000000000000000000000001000000000 -0000000000000000000000000000001000000000000000000000000000000000000000 -1000000000000000000000000000000000000001100000000000000000000000000000 -0000000001100000000000000000000000000000000000000110000000000000000000 -0000000000000000000110000000000000000000000000000000000000011000000000 -0000000000000000000000000000011000000000000000000000000000000000000001 -1111110000000000000000000000000000011111 \ No newline at end of file +0000000000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000111001110111011100011001110111 +0100111010100010011000000000000000000000000000000010010100001001001010 +0101000100010100001011001010010000000000000000000000000000001001010000 +1001001010010100010001010000101010101001000000000000000000000000000000 +1110011000100111001001011001100101000010100110111100000000000000000000 +0000000000100101000010010010100101000100010100001010001010010000000000 +0000000000000000000010010100001001001010010100010001010000101000101001 +0000000000000000000000000000001001010000100100101001010001000101000010 +1000101001000000000000000000000000000000100101110010010010011001000100 +0100111010100010100101000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000000000 +0000011001000011001110011100011000110011100011001000100111000101110000 +0000000000000010000100010000100101001010010100001001010010110110100000 +0100100000000000000000001000010001000010010100101001010000100101001010 +1010100000010010000000000000000000101101000101101110011100100101011011 +1001111010101001100001001000000000000000000010010100010010100001001010 +0101001010010100101000100001000100100000000000000000001001010001001010 +0001001010010100101001010010100010000100010010000000000000000000100101 +0001001010000100101001010010100101001010001000010001001000000000000000 +0000011001110011001000010010011000110010010100101000101110010100100000 +0000000000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000000000000000100000000000000000000 +0000000000000000000000000000000000000000000000000000000000001000000000 +0000000000000000000000000000000000000000000000000000000000000000000000 +0100000000000000000000000000000000000000000000000000000000000000000000 +0000000000001000000000000000000000000000000000000000000000000000000000 +0000000000000000000000010000000000000000000000000000000000000000000000 +0000000000000000000000000000000000100000000000000000000000000000000000 +0000000000000000000000000000000000000000000001000000111000000000000000 +0000000000000000000000000000000000000000000000000000000010001110000000 +0010000000000000000000000000000000000000000000000000000000000000000111 +1000000000010000000000000000000000000000000000000000000000000000000000 +0000000110000000000010000000000000000000000000000000000000000000000000 +0000000000000000010100000000010000000000000000000000000000000000000000 +0000000000000000000000000011001000000010000000000000000000000000000000 +0000000000000000000000000000000000001000010000010000000000000000000000 +0000000000000000000000000000000000000000000001100000100010000000000000 +0000000000000000000000000000000000000000000000000000000100000001010000 +0000000000000000000000000000000000000000000000000000000000000000010000 +0000100000000000000000000000000000000000000000000000000000000000000000 +0000010000000000000000001000000000000000000000000000000000000000000000 +0000000000000001000000000000000000100000000000000000000000000000000000 +0000000000000000000000000110000000000000000110000000000000000000000000 +0000000000000000000000000000000000001000000000000000010000000000000000 +0000000000000000000000000000000000000000000000110000000000000011000000 +0000000000000000000000000000000000000000000000000000000001000000000000 +0010000000000000000000000000000000000000000000000000000000000000000110 +0000000000011000000000000000000000000000000000000000000000000000000000 +0000000011100000000111000000000000000000000000000000000000000000000000 +0000000000000000000011100001110000000000000000000000000000000000000000 +0000000000000000000000000000000011111100000000000000000000000000000000 +0000000000 \ No newline at end of file diff --git a/samples/bouncing-logo.xcf b/samples/bouncing-logo.xcf index 1054ca8fbe8f8c5959ceea7fe3922b31860ae307..f121a7a9c18b477b48d4939e6cbbd6379cfec39f 100644 GIT binary patch literal 4029 zcmc(i%T63u5Qa~8Gc#P$nUI?l$&1cS!h(SrZeC)^k)kXjMamOk7{+4(`bSNIjO zaMpQ%Y_iBcZ;+>GwphfbE!*wMS5>E*wuGgYyzs!^^`BFx&Z+vV=LiPFgL}#SoxS9j zZm;L~`~YqW;qyC~2SLE&BQSmI59hwQb`$D822)@TeCoNZ`vm+4;sINCc18yW{lhV1 zC(aLV9rd@z{oUl(;rLGS?RVeHaqa_MIy~4O^gDy2;jYZd>OU8b#}D@V$?<4^xO;te zVQ;WA+8-Sy3u-z?gWKClSATlfao@he-AnXG-1m>~ZSM>Z2T8BX=)r3vKj{2?I2u=s$GUGYMeNo}pDtxxWlM0tk$v*OV4-`DE@HSk|D?TlVtdsq}_8jBB5x@wE@fJb3)2@2 zoO?fgE;e;hdagvcPK4=%Rk^yJ@U&T6aqSS@OX6b9DzN>OKep}UX)H>g*~u=AY91lE zg_4XNQKyi%4^i2)gi4iVVPetyc?5v*CDBcL|hWMiNw369n z#>%W3tLoi^Hp?zZH$*I2rLU21$kR&pVfxCXkdBMU2+ z6P`AUE3O@)dr4f(u>qOre##%)cJee9WzXznmvLLvIzn;_Wu+Zar;xV~QQ5SFQZFl^ z)az1WUr{Evh4z8B!uLaZBE2d4W7aFKK2u&IWLB)#OuM6xC_6Lpbf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