hi this video is part of a series in which i learn about programming a retro video game on the Commodore 64 in assembly the code i’m using is from the game supernatural written by Georg Rottensteiner you may also know the game as guns and ghosts this is not a tutorial just me learning game programming enjoy the video. Coders! good morning morning to me and not even that in the middle of the afternoon today is going to be a short one but we’re going to save the high score table 2 discs and I say specifically disk because the code was written with that in mind yeah it’s a choice doing it on tape a little bit more difficult because we’re the the thing is that we’re going to be writing a high score table piece of memory to a device and we’re going to overwrite the previous one so the thing is going to have a file name and it is going to be overwritten on the next iteration so doing that on tape is difficult because take doesn’t have a directory to you know you can find the file name in a chance you can do a single load and that’s it i’m going to show the the game running you know you’ll just have to that is not not really something to show i could show a filename on disk but I’m gonna go to the code if you’ve ever looked at handling files on disk on the Commodore then you know it’s it’s a bit our cake you work with logical file numbers so each number is given a handle this is not strange in programming at all it’s also given a device number now the device number could be a screen or good could be a printer it could be a tape or it could be a drive and then there’s the secondary address now the secondary addresses function depends on the device so it behaves differently for tape devices and it does for disc disc devices but we’ll see a couple of things and just go we’ll just go through let’s get started so the the trick is to save the high score data so the high score and the high school names to two discs and what is going to happen is that the bites starting from this location down to this location will just be written to disk and when we open the file we read the information again and write it to memory and save it from in the same space so this bit of memory is going to be manipulated by our disc routines so there’s a high score data end and that wasn’t there before but we need to be able to calculate the end address because we’re going to indicate the start button and address then a little bit higher up or we are going to handle names there’s let’s start with this is a dr number it is set at eight so that is eight means the first disk drive device described her have device number starting from eight and up there’s a high score file name which is just called high score you can change it just called that it also has a length its nine long 123456789 and there is another copy of that thing but then with this in front of it now again commons basic its original basic was you know it had a load in the saving of verifying that that was about it didn’t have a delete function so in order to delete a file off disc you had to open a channel for that device and send a command now this is a dis command this is the scratch command s 0 means scratch for device hero not going to go into why that is another device number that’s all ancient history but this is how it works and then the filename and we need an address for where the delete file name and so what you have to know is that if i send this as a command to the dr it’s going to delete this file name so what we will see is that when we save the high score table we delete the old one and then write the new one we didn’t delete the old one first the colonel would come back with an error messaging of file exists you know you can’t you can’t overwrite files by default so and that opens the door to the next thing that I because we’re going to scroll back up now we all the way at the top of the code now and you can see that there are some kernel routines we had some kernel routines already then they were deleted now they were added again and now they’re in there twice because you can see them they’re here as well note that these addresses md5 have beady in all those they point to a vector table vector table is basically a list of addresses that points to Colonel change that would allow Commodore to make changes to their to their code quite easily show the people could you know a previous code could still call this but the vector would point somewhere else you know they could just put in a new address in there and then your code would do the same sort of forward compatibility thing but not really necessary to know that but these are these are our routines i will go into what they do as we go to the code but you know know that they’re in their double its nobody was it’s just the way it is now when we start our code and scrolling down now to the first bit this also note that some of the addresses here they don’t have a label yet but be a hexadecimal is the 20 page address that holds the last device number used for calling a dr number here but it’s not fair because it’s a device number so so what we do is we load whatever is there and we store that to the variable drive number i don’t think we’ve seen that yet and maybe skip that actually let me see what I’m just gonna go to the Declaration of that all right oh ok it was a the draw ok so that’s this that was we didn’t skip it we we saw it actually now back to where we were 163 believe right so we initialize it with eight but what we’re doing is were initializing it again with a what we’re doing no no that’s not you where we’re loading a number we setting that to drive number if it was not 0 then we don’t need the default drive number but it was 0 ye we have we haven’t used any device yet then we just put an 88 was already in there so unless someone use dr nine to load the game then this will be nine and it won’t be over it by H so this is just a service to our friendly gamer and then we do load scores so we get right to the meat of the of the thing the the loading is as easy the first thing that we want to do is the suppress Colonel messages things like you know any any message that the colonel can give like error messages or loading or you know when you when you type load at the basic prompt the messages that follow are Colonel messages now when your in-game you don’t want those messages to appear so you can suppress them and writing 02 this to the accumulator than calling this routine means that you should press all messages there are some some alternatives you know just suppress basic messages or just suppress some I I’m i just read the manual on them but I didn’t remember all of the combinations but 0 means no messages at all then we start setting up our are loading so we use logical file number 15 which is arbitrary in my in my mind but 15 is one of those easy to remember numbers everybody uses channel 15 then we exhale drive number and why is the secondary address now when you are handling describes the secondary address zero and one they have the same meaning as when you load a file of this comma 8 comma 10 it will load the information to the address that you set which we’ll see in a minute because that’s what we’re doing there if we if we write a 1 here the colonel routine will look at the header the information coming from disk and write the information they’re so if the programmer definitely watch the data to go in a certain location you will include that information in the header and you load the program with a comma 1 and it’ll load to that location but we just want to set it by hand so we make the secondary address 20 now this secondary functions differently for tape drives and printers and whatnot so i’m not going to that just a thing that we need here now we need to set the filename we have to set that because we will get an error if we don’t the the name length which was nine we set the x and the y registers to the high score filename which we just chat which was just a high score name and then we call this routine done the logical file stuff we shut the name and now we have to set up the actual load so we 204 load we should the X&Y registered to where we want the stuff to load high score score and we call load and that’s when the magic happens if this if this works with without an error than the data will just be in memory the way to check whether it’s a worked or not is to check to carry to carry is set after loading then there is a load error notice by the way the German uh comment ok anyway if there is a load error then we return one if there was no lo there we return 0 no load error and one show hello there which means zero no lo their return one and that’s that shit so either it works or it doesn’t work and the load routine doesn’t do anything to repair it just didn’t work if it doesn’t work then it’s just going to be the old high score table that we programmed in this is going to be the default table then of course there is a moment at which and i’m going to have to look that up 737 and we go this hero notice by the way this text thing still in there hasn’t changed its unused just a bit of memory that’s sitting there anyway at the end of the high score table changing just before we jump to the title screen we call save scores this is the only call that we do with the end of that thing so when we change something in the table we do saves cause easier what is safe scores we haven’t seen it yet but here it is it’s right comes right after load first of all we’re going to have to delete the old file name so we take the high school delete file name end and subtract the high score delete file name so this was the the high score filename with the s0 and the colon and we actually calculate the difference this means that we edit this file and give the file a different name then this is going to function in the load a version we had fixed mine you know so this is how co-developed you know it’s not final and it’s not perfect but this works right action why we set to the address of the delete file name and then we call the set name will change this is part 1 then we set a logical file number 15 we said the dr number we set the secondary address to 15 now we’ve just talked about the secondary address if it’s zero or if it’s one of those are different functions then 2 214 are something else and 15 is a command so if we if we type 15 comma 8 15 it means that we’re going to send the command so we kept the logical file stuff we’re telling the colonel that we’re going to send the command and then we call the open routine so this doesn’t just open the file it opens the file but with the scratch command in front of it so this is going to attempt to remove the file it comes back with an error then for some reason or other the file couldn’t be opened probably it didn’t exist yet so in that case we’ll do error delete if if it the file was there it’s been deleted now and then something odd happens because we take the logical file number 15 and we call check out now it’s not really clear what this does to me because that file number was already open we had the dr number we had the secondary address where you just take the final number we close it and then we set the file number back to zero that to normal this bit of code i’m not entirely sure of their maybe it’s because the open with the command changes the file number or whatever anyway after we’ve we’ve deleted the file we have to sort of close the channel and then we set everything and then we can jump to shave national we have to put everything back to normal so this must be and I’m gonna have to look further into this but I couldn’t find anything logical about this why we have to change the file number 215 when it’s already there and then must be something in there oh I i may come back to that anyway in the air situation what happens we just write a character to the screen now this is all rock hard this is just a fixed address and a fixed number and we just right just to indicate you know to ourselves that something happened we just put something on the screen and we close so if we can delete the file historians if we could delete the file then we can just save what we already had now this is where the normal stuff happens again so we take the length of the of the filename start in the end we said the name stuff we shut the logical file stuff so in this case we use channel 0 15 and secondary address 0 we get the address of the high score as we were saving so but now we’re all of a sudden were using these are the addresses actually that are used to store that that information so all of a sudden we’re doing this by hand so we’re setting the address of the high score information then in c1 and c2 that’s the starts information and then the end information goes in x and y so this is appointed to the start we write that there and appointed to the end because you know we’re saving data so we have to indicate what is safe and how much and then we set the start address located in c1 c2 so we have to indicate where that is and then we call save let’s have a i’m going to have a cheeky peek at the at the save routine here FF d 8 i’m gonna make you were i probably can’t see this saves used transferred directly from round two and relations to say which performs open was appreciated by self s satnaam a file on the save it for without giving a filename I she takes the tapes don’t even have filenames a page 0.2 the starting address of the area has to be there can be shaped should be set up with a low by to the address first accumulate should be loaded with the h0 offset of that pointer ok so that that’s just how save works and these addresses I because I looked them up and they normally hold that information anyway so this is apparently how it works and then we call save and the thing does the precarious set a save area happened but we’re not we’re not doing anything with that sorry we couldn’t save your table nope just the end of story so I’m i don’t know how long have been going on a couple of minutes that’s it there’s no other changes in this in this episode so i hope you enjoyed it hope you learn something and i’ll see you in the next one don’t be square