Rev 539 | Blame | Compare with Previous | Last modification | View Log | Download
# file opened: union_like_structures.asm1 0000 ; This example shows one of possible ways to use multiple STRUCT definitions2 0000 ; as some kind of C-language-like "union"3 0000 ;4 0000 ; The "receive_buffer" is single block of memory large enough to accomodate5 0000 ; any of the specific "commands"6 0000 ;7 0000 ; RECEIVE_BUFFER_HEADER struct defines the initial header shared across all "commands"8 0000 ; RECEIVE_BUFFER_CMD_READ_REGS, RECEIVE_BUFFER_CMD_CONTINUE, RECEIVE_BUFFER_CMD_PED_TEST9 0000 ; then define the specific "commands" with their extended fields10 0000 ;11 0000 ; finally there is small fake process-like subroutine to show usage of the defined fields12 000013 0000 ; define the command structures14 0000 STRUCT RECEIVE_BUFFER_HEADER15 0000 ~ length DWORD 016 0000 ~ seq_no BYTE 017 0000 ~ command BYTE 018 0000 ENDS19 000020 0000 STRUCT RECEIVE_BUFFER_CMD_READ_REGS, RECEIVE_BUFFER_HEADER21 0000 ~ register_number BYTE 022 0000 ENDS23 000024 0000 STRUCT RECEIVE_BUFFER_CMD_CONTINUE, RECEIVE_BUFFER_HEADER25 0000 ~ bp1_enable BYTE 026 0000 ~ bp1_address WORD 027 0000 ~ bp2_enable BYTE 028 0000 ~ bp2_address WORD 029 0000 ENDS30 000031 0000 STRUCT RECEIVE_BUFFER_CMD_PED_TEST, RECEIVE_BUFFER_HEADER32 0000 ~ pattern BLOCK 25633 0000 ENDS34 000035 0000 ; find the structure with maximum size to define how long the receive_buffer should be36 0000 RB_MAX_SIZE = RECEIVE_BUFFER_HEADER37 0000 RB_MAX_SIZE = RB_MAX_SIZE >? RECEIVE_BUFFER_CMD_READ_REGS38 0000 RB_MAX_SIZE = RB_MAX_SIZE >? RECEIVE_BUFFER_CMD_CONTINUE39 0000 RB_MAX_SIZE = RB_MAX_SIZE >? RECEIVE_BUFFER_CMD_PED_TEST40 000041 0000 ; reserve the memory for the receive_buffer (one buffer for all)42 0000 ORG $800043 8000 00 00 00 00 receive_buffer RECEIVE_BUFFER_HEADER43 8004 00 0044 8006 00 00 00... .data BLOCK RB_MAX_SIZE - RECEIVE_BUFFER_HEADER, 045 810646 8106 ; definie alias labels for "receive_buffer" to access specific-command fields47 8106 rb_read_regs RECEIVE_BUFFER_CMD_READ_REGS = receive_buffer48 8106 rb_continue RECEIVE_BUFFER_CMD_CONTINUE = receive_buffer49 8106 rb_ped_test RECEIVE_BUFFER_CMD_PED_TEST = receive_buffer50 810651 8106 ; example of usage in code52 8106 ORG $C00053 C000 process_command:54 C000 3A 05 80 ld a,(receive_buffer.command)55 C003 FE 01 cp 156 C005 20 04 jr nz,.not_read_regs57 C007 ; CMD_READ_REGS specific code58 C007 3A 06 80 ld a,(rb_read_regs.register_number)59 C00A C7 rst 060 C00B .not_read_regs:61 C00B FE 02 cp 262 C00D 20 10 jr nz,.not_continue63 C00F ; CMD_CONTINUE specific code64 C00F 2A 07 80 ld hl,(rb_continue.bp1_address)65 C012 ED 5B 0A 80 ld de,(rb_continue.bp2_address)66 C016 ED 4B 06 80 ld bc,(rb_continue.bp1_enable) ; C = bp1_enable67 C01A 3A 09 80 ld a,(rb_continue.bp2_enable)68 C01D 47 ld b,a ; B = bp2_enable69 C01E C7 rst 070 C01F .not_continue:71 C01F ; must be RECEIVE_BUFFER_CMD_PED_TEST then72 C01F 21 06 80 ld hl,rb_ped_test.pattern73 C022 01 5B 00 ld bc,$5B ; C = ZX Next sprite pattern upload port, B = 0 (256x)74 C025 ED B3 otir ; upload pattern to active pattern slot75 C027 C7 rst 076 C028# file closed: union_like_structures.asmValue Label------ - -----------------------------------------------------------0xC000 X process_command0xC01F process_command.not_continue0xC00B process_command.not_read_regs0x8000 X rb_continue0x8007 rb_continue.bp1_address0x8006 rb_continue.bp1_enable0x800A rb_continue.bp2_address0x8009 rb_continue.bp2_enable0x0106 RB_MAX_SIZE0x8000 X rb_ped_test0x8006 rb_ped_test.pattern0x8000 X rb_read_regs0x8006 rb_read_regs.register_number0x8000 receive_buffer0x8005 receive_buffer.command0x8006 X receive_buffer.data0x8000 X receive_buffer.length0x8004 X receive_buffer.seq_no0x000C RECEIVE_BUFFER_CMD_CONTINUE0x0007 X RECEIVE_BUFFER_CMD_CONTINUE.bp1_address0x0006 X RECEIVE_BUFFER_CMD_CONTINUE.bp1_enable0x000A X RECEIVE_BUFFER_CMD_CONTINUE.bp2_address0x0009 X RECEIVE_BUFFER_CMD_CONTINUE.bp2_enable0x0106 RECEIVE_BUFFER_CMD_PED_TEST0x0006 X RECEIVE_BUFFER_CMD_PED_TEST.pattern0x0007 RECEIVE_BUFFER_CMD_READ_REGS0x0006 X RECEIVE_BUFFER_CMD_READ_REGS.register_number0x0006 RECEIVE_BUFFER_HEADER0x0005 X RECEIVE_BUFFER_HEADER.command0x0000 X RECEIVE_BUFFER_HEADER.length0x0004 X RECEIVE_BUFFER_HEADER.seq_no