📄 old.asm
字号:
mov r0, #state_next acall lexer jnc check_prev acall button_next retcheck_prev: mov dptr, #str_previous mov r0, #state_prev acall lexer jnc check_play acall button_prev retcheck_play: mov dptr, #str_play mov r0, #state_play acall lexer jnc check_all_done acall button_play retcheck_all_done: retquit: acall ide_sleep lcall newline mov a, status lcall phex lcall newline ljmp 0x006D ;go back to PM2;A simple fixed-string lexer. For each character received from an;input stream, this routine is called once for every string which;is to be recognized within the input stream.; Input registers:; r0 Memory location to the index variable for this; particular string.; r2 The character we received.; dptr Pointer to the string pattern we want to match.; Output registers:; c Carry set if complete string found, clear if no match yet.; mem Value @r0 adjusted appropriately for the next call.lexer: mov a, @r0 inc a ;don't look at length byte movc a, @a+dptr ;get character from string clr c subb a, r2 jnz lex_nope ;at this point, it matches the string, so now ;the question becomes "are we at the end?" inc @r0 ;advance index clr a movc a, @a+dptr ;get string length clr c subb a, @r0 ;subtract index jz lex_match ;we're not at the end of the string yet clr c retlex_match: ;we've found all of the string now mov @r0, a ;reset index automatically setb c retlex_nope: ;the received character doesn't match the string ;so we set the index back to zero, and return with ;carry clear mov @r0, #0 clr c ret.equ hc165_load_pin, 0x94.equ hc165_clk_pin, 0x95.equ hc165_data_pin, 0x96read_buttons: clr hc165_load_pin nop setb hc165_load_pin mov a, #255 mov c, hc165_data_pin clr hc165_clk_pin rlc a setb hc165_clk_pin mov c, hc165_data_pin clr hc165_clk_pin rlc a setb hc165_clk_pin mov c, hc165_data_pin clr hc165_clk_pin rlc a setb hc165_clk_pin mov c, hc165_data_pin clr hc165_clk_pin rlc a setb hc165_clk_pin mov c, hc165_data_pin clr hc165_clk_pin rlc a setb hc165_clk_pin mov c, hc165_data_pin rlc a cpl a ret;button 1: 02 forward;button 2: 10 play;button 3: 20 previous;button 4: 08 random;button 5: 01 vol_plus;button 6: 04 vol_minuscheck_buttons: acall read_buttons cjne a, button_state, ckb2 retckb2: mov b, button_state mov button_state, a jnb acc.4, ckb3 jnb b.4, button_playckb3: jnb acc.1, ckb4 jnb b.1, button_nextckb4: jnb acc.5, ckb5 jnb b.5, button_prevckb5: jnb acc.3, ckb6 jnb b.3, button_randckb6: jnb acc.0, ckb7 jnb b.0, button_vol_upckb7: jnb acc.2, ckb8 jnb b.2, button_vol_downckb8: retbutton_play: jb playing_flag, button_pause mov dptr, #mesg_play lcall pstr setb playing_flag clr paused_flag retbutton_pause: mov dptr, #mesg_pause lcall pstr setb paused_flag clr playing_flag retbutton_next: mov dptr, #mesg_next lcall pstr setb next_flag retbutton_prev: mov dptr, #mesg_prev lcall pstr setb prev_flag retbutton_rand: mov dptr, #mesg_rand lcall pstr cpl rand_mode_flag jb rand_mode_flag, brand_on mov a, #'f' lcall cout lcall cout ljmp newlinebrand_on: mov a, #'n' lcall cout ljmp newlinebutton_vol_up: mov dptr, #mesg_vol_up lcall pstr mov r4, #0x46 mov dptr, #sta013_rd lcall call_bank1 ;read current left attenuation mov a, r3 jz vup2 dec r3vup2: ;mov a, r3 ;lcall phex ;need to check for max attenuation mov dptr, #sta013_wr lcall call_bank1 ;write left attenuation mov r4, #0x48 mov dptr, #sta013_wr lcall call_bank1 ;write right attenuation retbutton_vol_down: mov dptr, #mesg_vol_down lcall pstr mov r4, #0x46 mov dptr, #sta013_rd lcall call_bank1 ;read current left attenuation mov a, r3 add a, #158 jc vdown2 inc r3vdown2: ;mov a, r3 ;lcall phex ;need to check for max attenuation mov dptr, #sta013_wr lcall call_bank1 ;write left attenuation mov r4, #0x48 mov dptr, #sta013_wr lcall call_bank1 ;write right attenuation retspace: mov a, #' ' ljmp cout ;print a 32 bit number stored @r0phex32_at_r0: inc r0 inc r0 inc r0 mov a, @r0 lcall phex dec r0 mov a, @r0 lcall phex dec r0 mov a, @r0 lcall phex dec r0 mov a, @r0 lcall phex ret;*************************************************************;** **;** FAT32 Filesystem **;** **;************************************************************* ;lookup the next cluster of a file, given the current ;cluster in r2 to r5. The next cluser is returned ;in r2 to r5.fat32_next_cluster: ;mov r0, #2 ;acall phex32_at_r0 ;mov a, #'-' ;lcall cout mov a, r2 push acc ;keep the lowest byte on the stack rlc a mov a, r3 ;rotate bits 8 to 31 into r2 to r5 rlc a mov r2, a mov a, r4 rlc a mov r3, a mov a, r5 anl a, #15 rlc a mov r4, a clr a rlc a mov r5, a ;mov r0, #2 ;acall phex32_at_r0 ;mov a, #'-' ;lcall cout ;add reserved_sector_count mov a, r2 add a, reserved_sector_count+0 mov r2, a mov a, r3 addc a, reserved_sector_count+1 mov r3, a mov a, r4 addc a, #0 mov r4, a mov a, r5 addc a, #0 mov r5, a ;mov r0, #2 ;acall phex32_at_r0 ;mov a, #'-' ;lcall cout ;add partition_first_sector mov a, r2 add a, partition_first_sector+0 mov r2, a mov a, r3 addc a, partition_first_sector+1 mov r3, a mov a, r4 addc a, partition_first_sector+2 mov r4, a mov a, r5 addc a, partition_first_sector+3 mov r5, a ;now r2 to r5 points to sector of the FAT mov lba+0, r2 mov lba+1, r3 mov lba+2, r4 mov lba+3, r5 ;mov r0, #2 ;acall phex32_at_r0 ;mov a, #'-' ;lcall cout ;read the sector from the FAT acall ide_read_sector lcall ide_drq lcall grab_data ;acall print_sector_buffer pop acc ;retrieve low byte anl a, #0x7F ;only need the low 7 bits clr c rlc a ;mult by 4 for offset in sector rlc a mov r2, a clr a rlc a mov r3, a ;add the offset to the buffer addr mov a, #sector_buffer & 255 add a, r2 mov dpl, a mov a, #sector_buffer >> 8 addc a, r3 mov dph, a movx a, @dptr mov r2, a inc dptr movx a, @dptr mov r3, a inc dptr movx a, @dptr mov r4, a inc dptr movx a, @dptr anl a, #15 mov r5, a ;mov r0, #2 ;acall phex32_at_r0 ;lcall newline ret ;convert a cluster number in r2 to r5 to a LBA address ;and put it into LBA.fat32_cluster_to_lba: clr c mov a, r2 ;first, subtract 2 subb a, #2 mov lba+0, a mov a, r3 subb a, #0 mov lba+1, a mov a, r4 subb a, #0 mov lba+2, a mov a, r5 anl a, #15 subb a, #0 mov lba+3, a ;now multiply by sectors_per_cluster ;always power of two, so use shifts mov r0, sectors_per_clusterf32_c2l_1: mov a, r0 clr c rrc a mov r0, a jz f32_c2l_2 clr c mov a, lba+0 rlc a mov lba+0, a mov a, lba+1 rlc a mov lba+1, a mov a, lba+2 rlc a mov lba+2, a mov a, lba+3 rlc a mov lba+3, a sjmp f32_c2l_1f32_c2l_2: mov a, lba+0 add a, first_data_sector+0 mov lba+0, a mov a, lba+1 addc a, first_data_sector+1 mov lba+1, a mov a, lba+2 addc a, first_data_sector+2 mov lba+2, a mov a, lba+3 addc a, first_data_sector+3 mov lba+3, a ret ;return C=0 if r2 to r5 contain an EOC marker, or C=1 ;if they do not.fat32_is_eoc: clr c mov a, r2 subb a, #0xF8 mov a, r3 subb a, #0xFF mov a, r4 subb a, #0xFF mov a, r5 anl a, #15 subb a, #0x0F ret ;advance the "dir_position_XXX" variables to the next file ;Carry is set if end of directory, clear if not at the endfat32_next_file: ;more forward one position within the current sector mov a, dir_position_offset inc a anl a, #15 mov dir_position_offset, a jnz f32_nf_done ;move to the next sector within the cluster mov r2, sectors_per_cluster dec r2 mov a, dir_position_sector inc a anl a, r2 mov dir_position_sector, a jnz f32_nf_done ;move to the next cluster mov r2, dir_position_cluster+0 mov r3, dir_position_cluster+1 mov r4, dir_position_cluster+2 mov r5, dir_position_cluster+3 acall fat32_next_cluster acall fat32_is_eoc jnc f32_nf_end mov dir_position_cluster+0, r2 mov dir_position_cluster+1, r3 mov dir_position_cluster+2, r4 mov dir_position_cluster+3, r5f32_nf_done: clr c retf32_nf_end: mov dir_position_cluster+0, root_dir_cluster+0 mov dir_position_cluster+1, root_dir_cluster+1 mov dir_position_cluster+2, root_dir_cluster+2 mov dir_position_cluster+3, root_dir_cluster+3 setb c ret ;back up the "dir_position_XXX" variables to the previous file ;Carry is set if beginning of directory, clear if not at the beginningfat32_prev_file: ;back up one position within the current sector mov a, dir_position_offset dec a anl a, #15 mov dir_position_offset, a orl a, #0xF0 cpl a jnz f32_pf_done ;back up one sector within the current cluster mov r2, sectors_per_cluster dec r2 mov a, r2 cpl a mov r3, a mov a, dir_position_sector dec a anl a, r2 mov dir_position_sector, a orl a, r3 cpl a jz f32_pf_prev_clusterf32_pf_done: clr c retf32_pf_prev_cluster: ;now for the really hard part... find the previous cluster! ;FAT32 doesn't have any easy way to figure out the previous ;cluster, and since we don't remember it (someday when the ;DRAM controller is supported we will), the only choice is ;to follow the cluster chain from the beginning until we get ;to the current spot, and see what the previous cluster was. ;"tmp_var" will remember the previous cluster, and we init ;it with the EOC marker, in case there is no previous cluster mov tmp_var+0, 0xFF mov tmp_var+1, 0xFF mov tmp_var+2, 0xFF mov tmp_var+3, 0x0F mov r2, root_dir_cluster+0 mov r3, root_dir_cluster+1 mov r4, root_dir_cluster+2 mov r5, root_dir_cluster+3f32_pf_loop: ;mov r0, #tmp_var ;acall phex32_at_r0 ;acall space ;mov r0, #dir_position_cluster ;acall phex32_at_r0 ;acall space ;mov r0, #2 ;acall phex32_at_r0 ;lcall newline ;if r2/r5 match "dir_position_cluster", then we're done searching mov a, r2 cjne a, dir_position_cluster+0, f32_pf_nxt mov a, r3 cjne a, dir_position_cluster+1, f32_pf_nxt mov a, r4 cjne a, dir_position_cluster+2, f32_pf_nxt mov a, r5 cjne a, dir_position_cluster+3, f32_pf_nxt sjmp f32_pf_clusterf32_pf_nxt: mov tmp_var+0, r2 mov tmp_var+1, r3 mov tmp_var+2, r4 mov tmp_var+3, r5 acall fat32_next_cluster acall fat32_is_eoc ;should never hit the end! jnc f32_pf_error sjmp f32_pf_loopf32_pf_cluster: ;at this point, "tmp_var" has the previous cluster. All ;we need to do is make sure it really is a valid cluster. mov r2, tmp_var+0 mov r3, tmp_var+1 mov r4, tmp_var+2 mov r5, tmp_var+3 acall fat32_is_eoc jnc f32_pf_end mov dir_position_cluster+0, tmp_var+0 mov dir_position_cluster+1, tmp_var+1 mov dir_position_cluster+2, tmp_var+2 mov dir_position_cluster+3, tmp_var+3 sjmp f32_pf_donef32_pf_error:f32_pf_end: mov dir_position_cluster+0, root_dir_cluster+0 mov dir_position_cluster+1, root_dir_cluster+1 mov dir_position_cluster+2, root_dir_cluster+2 mov dir_position_cluster+3, root_dir_cluster+3 mov dir_position_sector, #0 mov dir_position_offset, #0 setb c ret ;get the 32 bytes of info about the current file. ;the 32 bytes are always returned at the beginning ;of the sector buffer.fat32_get_dir_info: ;find the first sector of cluster mov r2, dir_position_cluster+0 mov r3, dir_position_cluster+1 mov r4, dir_position_cluster+2 mov r5, dir_position_cluster+3 acall fat32_cluster_to_lba ;add the sector offset with cluster mov a, lba+0 add a, dir_position_sector mov lba+0, a mov a, lba+1 addc a, #0 mov lba+1, a mov a, lba+2 addc a, #0 mov lba+2, a mov a, lba+3 addc a, #0 mov lba+3, a ;read the sector acall ide_read_sector lcall ide_drq lcall grab_data ;set DPTR to point to dir entry mov a, dir_position_offset anl a, #15 swap a clr c rlc a ;mult by 32 mov r2, a clr a rlc a mov r3, a mov a, #sector_buffer & 255 add a, r2 mov dpl, a mov a, #sector_buffer >> 8 addc a, r3 mov dph, a ;copy the filename mov r0, #filenamef32_gdi2: movx a, @dptr inc dptr mov @r0, a inc r0 cjne r0, #filename+11, f32_gdi2 ;copy the attrib movx a, @dptr mov attrib, a ;copy first cluster number mov a, dpl add a, #9 mov dpl, a mov a, dph addc a, #0 mov dph, a movx a, @dptr inc dptr mov cluster+2, a movx a, @dptr inc dptr mov cluster+3, a inc dptr ;skip write time and date inc dptr inc dptr inc dptr movx a, @dptr inc dptr mov cluster+0, a movx a, @dptr inc dptr mov cluster+1, a ;copy file size movx a, @dptr inc dptr mov size+0, a movx a, @dptr inc dptr mov size+1, a movx a, @dptr inc dptr mov size+2, a movx a, @dptr inc dptr mov size+3, a ret ;return C=1 if it's a MP3 file, or C=0 if not
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -