diff --git a/src/channels.h b/src/channels.h index f0dbdb8..1a1e18a 100644 --- a/src/channels.h +++ b/src/channels.h @@ -23,19 +23,19 @@ struct Chn { + int pattptr; + unsigned short freq; + unsigned short pulse; unsigned char trans; unsigned char instr; unsigned char note; unsigned char lastnote; unsigned char newnote; - int pattptr; unsigned char pattnum; unsigned char songptr; unsigned char repeat; - unsigned short freq; unsigned char gate; unsigned char wave; - unsigned short pulse; unsigned char ptr[2]; unsigned char pulsetime; unsigned char wavetime; @@ -47,9 +47,9 @@ struct Chn unsigned char newcmddata; unsigned char tick; unsigned char tempo; - unsigned char mute; - unsigned char advance; unsigned char gatetimer; + bool advance; + bool mute; }; #ifndef CHANNELS_C diff --git a/src/play.cpp b/src/play.cpp index f51a0ba..e57f1af 100644 --- a/src/play.cpp +++ b/src/play.cpp @@ -135,7 +135,7 @@ void releasenote(int chnnum) void mutechannel(int chnnum) { - chn[chnnum].mute ^= 1; + chn[chnnum].mute = !chn[chnnum].mute; } bool isplaying() @@ -172,7 +172,7 @@ void playroutine() cptr->cmddata = 0; cptr->newcommand = 0; cptr->newcmddata = 0; - cptr->advance = 1; + cptr->advance = true; cptr->wave = 0; cptr->ptr[WTBL] = 0; cptr->newnote = 0; @@ -208,7 +208,7 @@ void playroutine() break; case PLAY_PATTERN: - cptr->advance = 0; + cptr->advance = false; cptr->pattptr = startpattpos * 4; cptr->pattnum = epnum[c]; if (cptr->pattptr >= (getPattlen(cptr->pattnum) * 4)) @@ -973,7 +973,7 @@ void playroutine_stereo() cptr->cmddata = 0; cptr->newcommand = 0; cptr->newcmddata = 0; - cptr->advance = 1; + cptr->advance = true; cptr->wave = 0; cptr->ptr[WTBL] = 0; cptr->newnote = 0; @@ -1009,7 +1009,7 @@ void playroutine_stereo() break; case PLAY_PATTERN: - cptr->advance = 0; + cptr->advance = false; cptr->pattptr = startpattpos * 4; cptr->pattnum = epnum[c]; if (cptr->pattptr >= (getPattlen(cptr->pattnum) * 4)) diff --git a/src/reloc.cpp b/src/reloc.cpp index 7ac8d50..3dddc93 100644 --- a/src/reloc.cpp +++ b/src/reloc.cpp @@ -73,6 +73,14 @@ enum class ErrorType JUMP }; +struct TabError +{ + ErrorType type = ErrorType::NONE; + Cause cause = Cause::NONE; + int source1 = 0; + int source2 = 0; +}; + #define MAX_BYTES_PER_ROW 16 const char *playeroptname[MAX_OPTIONS] = @@ -175,15 +183,111 @@ void error(const char *msg) waitkeynoupdate(); } +void table_error(TabError terr) +{ + clearscreen(); + switch (terr.type) + { + case ErrorType::JUMP: + std::snprintf(textbuffer, MAX_PATHNAME, "TABLE POINTER POINTS TO A JUMP! "); + break; + + case ErrorType::OVERFLOW: + std::snprintf(textbuffer, MAX_PATHNAME, "TABLE EXECUTION OVERFLOWS! "); + break; + + case ErrorType::NONE: + // unreachable + break; + } + switch (terr.cause) + { + case Cause::PATTERN: + std::snprintf(textbuffer, MAX_PATHNAME + std::strlen(textbuffer), "(PATTERN %02X, ROW %02d)", terr.source1, terr.source2); + break; + + case Cause::WAVE_CMD: + std::snprintf(textbuffer, MAX_PATHNAME + std::strlen(textbuffer), "WAVETABLE CMD (ROW %02X, ", terr.source1); + goto TABLETYPE; + + case Cause::INSTRUMENT: + std::snprintf(textbuffer, MAX_PATHNAME + std::strlen(textbuffer), "(INSTRUMENT %02X, ", terr.source1); +TABLETYPE: + switch (terr.source2) + { + case WTBL: + std::strcat(textbuffer, "WAVE"); + break; + + case PTBL: + std::strcat(textbuffer, "PULSE"); + break; + + case FTBL: + std::strcat(textbuffer, "FILTER"); + break; + } + std::strcat(textbuffer, ")"); + break; + + case Cause::NONE: + // unreachable + break; + } + printtextc(MAX_ROWS/2, colors.CTITLE, textbuffer); + + fliptoscreen(); + waitkeynoupdate(); +} + +void initreloc() +{ + noeffects = 1; + nogate = 1; + nofilter = 1; + nofiltermod = 1; + nopulse = 1; + nopulsemod = 1; + nowavedelay = 1; + nowavecmd = 1; + norepeat = 1; + notrans = 1; + noportamento = 1; + notoneporta = 1; + novib = 1; + noinsvib = 1; + nosetad = 1; + nosetsr = 1; + nosetwave = 1; + nosetwaveptr = 1; + nosetpulseptr = 1; + nosetfiltptr = 1; + nosetfiltcutoff = 1; + nosetfiltctrl = 1; + nosetmastervol = 1; + nofunktempo = 1; + noglobaltempo = 1; + nochanneltempo = 1; + nofirstwavecmd = 1; + nocalculatedspeed = 1; + nonormalspeed = 1; + nozerospeed = 1; + + std::memset(pattused, 0, sizeof pattused); + std::memset(instrused, 0, sizeof instrused); + std::memset(chnused, 0, sizeof chnused); + std::memset(tableused, 0, sizeof tableused); + std::memset(tablemap, 0, sizeof tablemap); + + tableerror = ErrorType::NONE; +} + void relocator(const char* filename) { unsigned char *packeddata = nullptr; - const char *playername = "player.s"; + const char *playername = (config.adparam < 0xf000) ? "player.s" : "altplayer.s"; - ErrorType tableerrortype = ErrorType::NONE; - Cause tableerrorcause = Cause::NONE; - int tableerrorsource1 = 0; - int tableerrorsource2 = 0; + TabError taberr; int patterns = 0; int songs = 0; int instruments = 0; @@ -227,45 +331,10 @@ void relocator(const char* filename) int firstnote = MAX_NOTES-1; int lastnote = 0; int patternlastnote = 0; - noeffects = 1; - nogate = 1; - nofilter = 1; - nofiltermod = 1; - nopulse = 1; - nopulsemod = 1; - nowavedelay = 1; - nowavecmd = 1; - norepeat = 1; - notrans = 1; - noportamento = 1; - notoneporta = 1; - novib = 1; - noinsvib = 1; - nosetad = 1; - nosetsr = 1; - nosetwave = 1; - nosetwaveptr = 1; - nosetpulseptr = 1; - nosetfiltptr = 1; - nosetfiltcutoff = 1; - nosetfiltctrl = 1; - nosetmastervol = 1; - nofunktempo = 1; - noglobaltempo = 1; - nochanneltempo = 1; - nofirstwavecmd = 1; - nocalculatedspeed = 1; - nonormalspeed = 1; - nozerospeed = 1; - stopsong(); + initreloc(); - std::memset(pattused, 0, sizeof pattused); - std::memset(instrused, 0, sizeof instrused); - std::memset(chnused, 0, sizeof chnused); - std::memset(tableused, 0, sizeof tableused); - std::memset(tablemap, 0, sizeof tablemap); - tableerror = ErrorType::NONE; + stopsong(); parse_init(); buf_free(&src); @@ -329,18 +398,20 @@ void relocator(const char* filename) } } - // Optimize amount of used channels - if (!chnused[2]) - channels = 2; - if ((!chnused[1]) && (!chnused[2])) - channels = 1; - if (!songs) { error("NO SONGS, NO DATA TO SAVE!"); goto PRCLEANUP; } + // Optimize amount of used channels + for (int c = maxChns; c; c--) + { + if (chnused[c]) + break; + channels = c; + } + // Build the pattern-mapping // Instrument 1 is always used instrused[1] = 1; @@ -398,12 +469,12 @@ void relocator(const char* filename) lastnote = newfirstnote; } } - if ((tableerror != ErrorType::NONE) && (tableerrortype == ErrorType::NONE)) + if ((tableerror != ErrorType::NONE) && (taberr.type == ErrorType::NONE)) { - tableerrortype = tableerror; - tableerrorcause = Cause::PATTERN; - tableerrorsource1 = c; - tableerrorsource2 = d; + taberr.type = tableerror; + taberr.cause = Cause::PATTERN; + taberr.source1 = c; + taberr.source2 = d; } } } @@ -446,12 +517,12 @@ void relocator(const char* filename) tableerror = ErrorType::NONE; exectable(d, song.instr[c].ptr[d]); if (d == STBL) calcspeedtest(song.instr[c].ptr[d]); - if ((tableerror != ErrorType::NONE) && (tableerrortype == ErrorType::NONE)) + if ((tableerror != ErrorType::NONE) && (taberr.type == ErrorType::NONE)) { - tableerrortype = tableerror; - tableerrorcause = Cause::INSTRUMENT; - tableerrorsource1 = c; - tableerrorsource2 = d; + taberr.type = tableerror; + taberr.cause = Cause::INSTRUMENT; + taberr.source1 = c; + taberr.source2 = d; } } } @@ -497,12 +568,12 @@ void relocator(const char* filename) if (d != -1) exectable(d, song.rtable[WTBL][c]); - if ((tableerror != ErrorType::NONE) && (tableerrortype == ErrorType::NONE)) + if ((tableerror != ErrorType::NONE) && (taberr.type == ErrorType::NONE)) { - tableerrortype = tableerror; - tableerrorcause = Cause::WAVE_CMD; - tableerrorsource1 = c+1; - tableerrorsource2 = d; + taberr.type = tableerror; + taberr.cause = Cause::WAVE_CMD; + taberr.source1 = c+1; + taberr.source2 = d; } } } @@ -523,61 +594,9 @@ void relocator(const char* filename) } // Check for table errors - if (tableerrorcause != Cause::NONE) + if (taberr.cause != Cause::NONE) { - clearscreen(); - switch(tableerrortype) - { - case ErrorType::JUMP: - std::snprintf(textbuffer, MAX_PATHNAME, "TABLE POINTER POINTS TO A JUMP! "); - break; - - case ErrorType::OVERFLOW: - std::snprintf(textbuffer, MAX_PATHNAME, "TABLE EXECUTION OVERFLOWS! "); - break; - - case ErrorType::NONE: - // unreachable - break; - } - switch (tableerrorcause) - { - case Cause::PATTERN: - std::snprintf(textbuffer, MAX_PATHNAME + std::strlen(textbuffer), "(PATTERN %02X, ROW %02d)", tableerrorsource1, tableerrorsource2); - break; - - case Cause::WAVE_CMD: - std::snprintf(textbuffer, MAX_PATHNAME + std::strlen(textbuffer), "WAVETABLE CMD (ROW %02X, ", tableerrorsource1); - goto TABLETYPE; - - case Cause::INSTRUMENT: - std::snprintf(textbuffer, MAX_PATHNAME + std::strlen(textbuffer), "(INSTRUMENT %02X, ", tableerrorsource1); -TABLETYPE: - switch (tableerrorsource2) - { - case WTBL: - std::strcat(textbuffer, "WAVE"); - break; - - case PTBL: - std::strcat(textbuffer, "PULSE"); - break; - - case FTBL: - std::strcat(textbuffer, "FILTER"); - break; - } - std::strcat(textbuffer, ")"); - break; - - case Cause::NONE: - // unreachable - break; - } - printtextc(MAX_ROWS/2, colors.CTITLE, textbuffer); - - fliptoscreen(); - waitkeynoupdate(); + table_error(taberr); goto PRCLEANUP; } @@ -717,7 +736,7 @@ void relocator(const char* filename) // Sound effect or ghostreg players always use full 3 channels if ((config.playerversion & PLAYER_SOUNDEFFECTS) || (config.playerversion & PLAYER_FULLBUFFERED) || (config.playerversion & PLAYER_ZPGHOSTREGS)) - channels = 3; + channels = maxChns; // Allocate memory for song-orderlists songtblsize = songs*6; @@ -1222,9 +1241,6 @@ void relocator(const char* filename) } // Insert source code of player - if (config.adparam >= 0xf000) - playername = "altplayer.s"; - if (!insertfile(playername)) { error("COULD NOT OPEN PLAYROUTINE!"); @@ -1441,7 +1457,8 @@ void relocator(const char* filename) } } - SKIPTABLE: ; +SKIPTABLE: + {} } // Insert orderlists @@ -1470,7 +1487,7 @@ void relocator(const char* filename) } #endif // Assemble; on error fail in a rude way (the parser does so too) - if (assemble(&src, &dest)) exit(1); + if (assemble(&src, &dest)) std::exit(EXIT_FAILURE); packeddata = (unsigned char*)buf_data(&dest); packedsize = buf_size(&dest); @@ -2257,12 +2274,9 @@ void calcspeedtest(unsigned char pos) void relocator_stereo(const char* filename) { unsigned char *packeddata = nullptr; - const char *playername = "player_s.s"; + const char *playername = (config.adparam < 0xf000) ? "player_s.s" : "altplayer_s.s"; - ErrorType tableerrortype = ErrorType::NONE; - Cause tableerrorcause = Cause::NONE; - int tableerrorsource1 = 0; - int tableerrorsource2 = 0; + TabError taberr; int patterns = 0; int songs = 0; int instruments = 0; @@ -2302,45 +2316,10 @@ void relocator_stereo(const char* filename) int firstnote = MAX_NOTES-1; int lastnote = 0; int patternlastnote = 0; - noeffects = 1; - nogate = 1; - nofilter = 1; - nofiltermod = 1; - nopulse = 1; - nopulsemod = 1; - nowavedelay = 1; - nowavecmd = 1; - norepeat = 1; - notrans = 1; - noportamento = 1; - notoneporta = 1; - novib = 1; - noinsvib = 1; - nosetad = 1; - nosetsr = 1; - nosetwave = 1; - nosetwaveptr = 1; - nosetpulseptr = 1; - nosetfiltptr = 1; - nosetfiltcutoff = 1; - nosetfiltctrl = 1; - nosetmastervol = 1; - nofunktempo = 1; - noglobaltempo = 1; - nochanneltempo = 1; - nofirstwavecmd = 1; - nocalculatedspeed = 1; - nonormalspeed = 1; - nozerospeed = 1; - stopsong(); + initreloc(); - std::memset(pattused, 0, sizeof pattused); - std::memset(instrused, 0, sizeof instrused); - std::memset(chnused, 0, sizeof chnused); - std::memset(tableused, 0, sizeof tableused); - std::memset(tablemap, 0, sizeof tablemap); - tableerror = ErrorType::NONE; + stopsong(); parse_init(); buf_free(&src); @@ -2403,19 +2382,21 @@ void relocator_stereo(const char* filename) songs++; } } -#if 0 - // Optimize amount of used channels - if (!chnused[2]) - channels = 2; - if ((!chnused[1]) && (!chnused[2])) - channels = 1; -#endif + if (!songs) { error("NO SONGS, NO DATA TO SAVE!"); goto PRCLEANUP_S; } +#if 0 + for (int c = maxChns; c; c--) + { + if (chnused[c]) + break; + channels = c; + } +#endif // Build the pattern-mapping // Instrument 1 is always used instrused[1] = 1; @@ -2473,12 +2454,12 @@ void relocator_stereo(const char* filename) lastnote = newfirstnote; } } - if ((tableerror != ErrorType::NONE) && (tableerrortype == ErrorType::NONE)) + if ((tableerror != ErrorType::NONE) && (taberr.type == ErrorType::NONE)) { - tableerrortype = tableerror; - tableerrorcause = Cause::PATTERN; - tableerrorsource1 = c; - tableerrorsource2 = d; + taberr.type = tableerror; + taberr.cause = Cause::PATTERN; + taberr.source1 = c; + taberr.source2 = d; } } } @@ -2521,12 +2502,12 @@ void relocator_stereo(const char* filename) tableerror = ErrorType::NONE; exectable(d, song.instr[c].ptr[d]); if (d == STBL) calcspeedtest(song.instr[c].ptr[d]); - if ((tableerror != ErrorType::NONE) && (tableerrortype == ErrorType::NONE)) + if ((tableerror != ErrorType::NONE) && (taberr.type == ErrorType::NONE)) { - tableerrortype = tableerror; - tableerrorcause = Cause::INSTRUMENT; - tableerrorsource1 = c; - tableerrorsource2 = d; + taberr.type = tableerror; + taberr.cause = Cause::INSTRUMENT; + taberr.source1 = c; + taberr.source2 = d; } } } @@ -2572,12 +2553,12 @@ void relocator_stereo(const char* filename) if (d != -1) exectable(d, song.rtable[WTBL][c]); - if ((tableerror != ErrorType::NONE) && (tableerrortype == ErrorType::NONE)) + if ((tableerror != ErrorType::NONE) && (taberr.type == ErrorType::NONE)) { - tableerrortype = tableerror; - tableerrorcause = Cause::WAVE_CMD; - tableerrorsource1 = c+1; - tableerrorsource2 = d; + taberr.type = tableerror; + taberr.cause = Cause::WAVE_CMD; + taberr.source1 = c+1; + taberr.source2 = d; } } } @@ -2598,61 +2579,9 @@ void relocator_stereo(const char* filename) } // Check for table errors - if (tableerrorcause != Cause::NONE) + if (taberr.cause != Cause::NONE) { - clearscreen(); - switch(tableerrortype) - { - case ErrorType::JUMP: - std::snprintf(textbuffer, MAX_PATHNAME, "TABLE POINTER POINTS TO A JUMP! "); - break; - - case ErrorType::OVERFLOW: - std::snprintf(textbuffer, MAX_PATHNAME, "TABLE EXECUTION OVERFLOWS! "); - break; - - case ErrorType::NONE: - // unreachable - break; - } - switch (tableerrorcause) - { - case Cause::PATTERN: - std::snprintf(textbuffer, MAX_PATHNAME + std::strlen(textbuffer), "(PATTERN %02X, ROW %02d)", tableerrorsource1, tableerrorsource2); - break; - - case Cause::WAVE_CMD: - std::snprintf(textbuffer, MAX_PATHNAME + std::strlen(textbuffer), "WAVETABLE CMD (ROW %02X, ", tableerrorsource1); - goto TABLETYPE_S; - - case Cause::INSTRUMENT: - std::snprintf(textbuffer, MAX_PATHNAME + std::strlen(textbuffer), "(INSTRUMENT %02X, ", tableerrorsource1); -TABLETYPE_S: - switch (tableerrorsource2) - { - case WTBL: - std::strcat(textbuffer, "WAVE"); - break; - - case PTBL: - std::strcat(textbuffer, "PULSE"); - break; - - case FTBL: - std::strcat(textbuffer, "FILTER"); - break; - } - std::strcat(textbuffer, ")"); - break; - - case Cause::NONE: - // unreachable - break; - } - printtextc(MAX_ROWS/2, colors.CTITLE, textbuffer); - - fliptoscreen(); - waitkeynoupdate(); + table_error(taberr); goto PRCLEANUP_S; } @@ -3285,9 +3214,6 @@ void relocator_stereo(const char* filename) } // Insert source code of player - if (config.adparam >= 0xf000) - playername = "altplayer_s.s"; - if (!insertfile(playername)) { error("COULD NOT OPEN PLAYROUTINE!"); @@ -3490,7 +3416,7 @@ void relocator_stereo(const char* filename) } SKIPTABLE_S: - ; + {} } // Insert orderlists @@ -3519,7 +3445,7 @@ void relocator_stereo(const char* filename) } #endif // Assemble; on error fail in a rude way (the parser does so too) - if (assemble(&src, &dest)) exit(1); + if (assemble(&src, &dest)) std::exit(EXIT_FAILURE); packeddata = (unsigned char*)buf_data(&dest); packedsize = buf_size(&dest); diff --git a/src/song.cpp b/src/song.cpp index a50ccdc..ed5f0ab 100644 --- a/src/song.cpp +++ b/src/song.cpp @@ -1586,7 +1586,7 @@ void clearsong(bool cs, bool cp, bool ci, bool ct, bool cn) for (int c = 0; c < maxChns; c++) { - chn[c].mute = 0; + chn[c].mute = false; if (config.multiplier) chn[c].tempo = config.multiplier*6-1; else diff --git a/src/tools/ltreloc.cpp b/src/tools/ltreloc.cpp index 44f4e44..b4b0631 100644 --- a/src/tools/ltreloc.cpp +++ b/src/tools/ltreloc.cpp @@ -115,11 +115,12 @@ int main(int argc, char **argv) // Reset channels/song initchannels(); - clearsong(true,true,true,true,true); + clearsong(true, true, true, true, true); char packedsongname[MAX_PATHNAME]; // get input- and output file names - if (argc >= 3) { + if (argc >= 3) + { std::strcpy(songfilename, argv[1]); std::strcpy(packedsongname, argv[2]); } else { @@ -128,7 +129,8 @@ int main(int argc, char **argv) } // Load song - if (std::strlen(songfilename)) { + if (std::strlen(songfilename)) + { loadsong(); if (std::strlen(loadedsongfilename) == 0) { @@ -141,7 +143,8 @@ int main(int argc, char **argv) } int c = (int)std::strlen(packedsongname); - if (c <= 0) { + if (c <= 0) + { std::fprintf(STDERR, "error: no output filename given.\n"); std::exit(EXIT_FAILURE); }