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 1054ca8..f121a7a 100644 Binary files a/samples/bouncing-logo.xcf and b/samples/bouncing-logo.xcf differ