x86asmban - Sokoban in an MBR
-
- Posts: 12
- Joined: Wed May 19, 2021 10:32 am
- Discord: Fahr
- GitHub: https://github.com/gboor
x86asmban - Sokoban in an MBR
Hello all,
I've been sitting on this one for a while. Finished it almost 2 years ago with the intention of doing a full write-up on it as a series of blog posts, but I've just not had the time.
Bottom line; I've built a fully (hopefully) working version of the game sokoban as an MBR using mode 13h for graphics and a PC speaker for audio. This was mainly an exploration project for myself to tinker with pure x86 asm, but I'm quite happy with the result.
Before I do a full write up/blog series about this, I figured I'd link it here in case anyone wants to look at it. Bear in mind that this is my first serious pure x86 asm project (though I've previously written drivers and other low-level stuff), so I'm not just looking for feedback on the actual functionality of the thing, but - perhaps even more - on the code, quality and design choices.
Perhaps too big an ask, but I have to start somewhere to get some feedback
Repo with more instructions is here; https://github.com/gboor/x86asmban
Thanks in advance to anyone who wants to have a look!
I've been sitting on this one for a while. Finished it almost 2 years ago with the intention of doing a full write-up on it as a series of blog posts, but I've just not had the time.
Bottom line; I've built a fully (hopefully) working version of the game sokoban as an MBR using mode 13h for graphics and a PC speaker for audio. This was mainly an exploration project for myself to tinker with pure x86 asm, but I'm quite happy with the result.
Before I do a full write up/blog series about this, I figured I'd link it here in case anyone wants to look at it. Bear in mind that this is my first serious pure x86 asm project (though I've previously written drivers and other low-level stuff), so I'm not just looking for feedback on the actual functionality of the thing, but - perhaps even more - on the code, quality and design choices.
Perhaps too big an ask, but I have to start somewhere to get some feedback
Repo with more instructions is here; https://github.com/gboor/x86asmban
Thanks in advance to anyone who wants to have a look!
-
- Member
- Posts: 5655
- Joined: Mon Mar 25, 2013 7:01 pm
Re: x86asmban - Sokoban in an MBR
The MBR is just the first sector of the disk, I don't think it counts as an MBR when you're using more than one sector of the disk.
Speaking of which, if you want to try booting it from USB instead of a floppy disk, you might have trouble because you don't have a partition table in your MBR. Some BIOSes are picky about that.
Oh, I've got some PCs I'd like to try this on...
-
- Posts: 12
- Joined: Wed May 19, 2021 10:32 am
- Discord: Fahr
- GitHub: https://github.com/gboor
Re: x86asmban - Sokoban in an MBR
I realize that and I actually did some research into that. While there doesn't seem to be any actual standard, bootloaders like Grub seem to use up to the first 64 sectors of a disk and, from what I gathered, this is generally considered to be ok. I might be wrong, but Grub definitely uses more than one sector. As do many other bootloaders... perhaps "bootloader" here is better than "MBR", though it's also not exactly a bootloader as it doesn't load anything...Octocontrabass wrote: ↑Sat Jan 11, 2025 10:30 pm The MBR is just the first sector of the disk, I don't think it counts as an MBR when you're using more than one sector of the disk.
Line 130 in mbr.asm has a fake "partition table", which should satisfy most BIOSes. Though it may require an actual entry; I've not tested this broadly on actual hardware (actually only on an old NAS that still has a disk drive; haven't tested it on a USB stick at all), but some emulators that complained about there being no partition table stopped complaining once I put that in.Octocontrabass wrote: ↑Sat Jan 11, 2025 10:30 pm Speaking of which, if you want to try booting it from USB instead of a floppy disk, you might have trouble because you don't have a partition table in your MBR. Some BIOSes are picky about that.
I am very curious about how (and IF) it runs on what actual hardware. As mentioned above; I don't have too many devices I can test this on. I've also not been able to load it from a USB stick despite trying; the requirements seem to be quite different in terms of what data needs to be provided in the first sector. I've also not really tried beyond a small experiment, it's still on my todo.
-
- Member
- Posts: 5655
- Joined: Mon Mar 25, 2013 7:01 pm
Re: x86asmban - Sokoban in an MBR
It depends on the PC, but some do indeed require an actual entry. They can be pretty picky about it, too, but you can usually satisfy them just by creating a partition in Windows and marking the partition as active (bootable).
The first PC I tried it on said "Error reading disk". I'd guess that's because you're telling the BIOS to read a floppy disk and I can't boot any PCs from a floppy disk at the moment.Octocontrabass wrote: ↑Sat Jan 11, 2025 10:30 pmI am very curious about how (and IF) it runs on what actual hardware.
-
- Posts: 12
- Joined: Wed May 19, 2021 10:32 am
- Discord: Fahr
- GitHub: https://github.com/gboor
Re: x86asmban - Sokoban in an MBR
That's a good catch... I've always only tested with the first disk drive, so dl being 0 has never affected anything - oops. I've rectified this in the github code now, it should save and restore the proper value of dl.Octocontrabass wrote: ↑Sun Jan 12, 2025 7:17 pm The first PC I tried it on said "Error reading disk". I'd guess that's because you're telling the BIOS to read a floppy disk and I can't boot any PCs from a floppy disk at the moment.
Just out of curiosity, what are you trying to boot it from? USB? HDD?
-
- Member
- Posts: 5655
- Joined: Mon Mar 25, 2013 7:01 pm
Re: x86asmban - Sokoban in an MBR
Indeed it does. Now it says "Error reading sector", which I'd guess is because this PC's BIOS does something that isn't what you expect.
HDD. (It's actually a CF card but the BIOS doesn't know that.)
-
- Posts: 12
- Joined: Wed May 19, 2021 10:32 am
- Discord: Fahr
- GitHub: https://github.com/gboor
Re: x86asmban - Sokoban in an MBR
I've removed the sector check. I guess it's silly to have that anyway if the read returns an OK status. I may still add handling for ah = 0x11 (data ECC corrected) at some point, but that seems like overkill for now.Octocontrabass wrote: ↑Tue Jan 14, 2025 12:48 pm Now it says "Error reading sector", which I'd guess is because this PC's BIOS does something that isn't what you expect.
That sounds like an interesting setup. I did some quick searching and found there's SD to SSD adapters as well, which will definitely help a lot with testing this stuff. I shall order one. Does your adapter hot-swap? Could you send me a link?Octocontrabass wrote: ↑Tue Jan 14, 2025 12:48 pm HDD. (It's actually a CF card but the BIOS doesn't know that.)
-
- Member
- Posts: 5655
- Joined: Mon Mar 25, 2013 7:01 pm
Re: x86asmban - Sokoban in an MBR
Now the game starts, but keyboard input is messed up. I see at least one incorrect assumption about BIOS behavior.
It's not an adapter, the PC in question has a CF slot instead of a 40-pin IDE connector. It definitely does not hot-swap!
-
- Posts: 12
- Joined: Wed May 19, 2021 10:32 am
- Discord: Fahr
- GitHub: https://github.com/gboor
Re: x86asmban - Sokoban in an MBR
This is a very odd one. I definitely didn't come up with the idea that int 0x16 clears CF, but I cannot find any reference to it. Strangely enough; it works in my various emulators... I've altered the code specifically clear CF when there's something in the buffer.Octocontrabass wrote: ↑Tue Jan 14, 2025 2:24 pm Now the game starts, but keyboard input is messed up. I see at least one incorrect assumption about BIOS behavior.
I've done a bit of searching and it looks like a great upgrade for old hardware since CF is basically IDE... I have an old machine I sometimes test stuff on and this looks like a smart thing to do. At least smarter than doing something SATA or USB with SD for old machines. I had no idea this was possible. Thanks for pointing it out, I'm gonna buy oneOctocontrabass wrote: ↑Tue Jan 14, 2025 2:24 pm It's not an adapter, the PC in question has a CF slot instead of a 40-pin IDE connector. It definitely does not hot-swap!
-
- Member
- Posts: 5655
- Joined: Mon Mar 25, 2013 7:01 pm
Re: x86asmban - Sokoban in an MBR
It's still acting really weird. It looks like you're expecting CF to be preserved across calls to INT 0x16 AH=0x01, but BIOS calls that modify one flag will usually modify all of them.
-
- Posts: 12
- Joined: Wed May 19, 2021 10:32 am
- Discord: Fahr
- GitHub: https://github.com/gboor
Re: x86asmban - Sokoban in an MBR
I've changed the routine to explicitly set or clear CF at the end now, after all the interrupts. Hopefully that fixes it. As usual; "it works for me", but that's clearly no guarantee.Octocontrabass wrote: ↑Thu Jan 16, 2025 8:47 pm It's still acting really weird. It looks like you're expecting CF to be preserved across calls to INT 0x16 AH=0x01, but BIOS calls that modify one flag will usually modify all of them.
If this still doesn't work I probably need to emulate this with a different BIOS. This should be possible with both Bochs and QEMU. What BIOS are you on?
-
- Member
- Posts: 5655
- Joined: Mon Mar 25, 2013 7:01 pm
Re: x86asmban - Sokoban in an MBR
It does seem to be fixed, I was able to play through a few levels without any trouble (aside from the rather steep difficulty curve). I did notice that the input buffer doesn't get flushed between levels, but you can just restart the level if the buffered input messes something up.
It's possible, but where will you get a different BIOS that works in a virtual machine?
It's an AMI Hi-Flex BIOS with a core date of July 15th, 1995.
-
- Posts: 12
- Joined: Wed May 19, 2021 10:32 am
- Discord: Fahr
- GitHub: https://github.com/gboor
Re: x86asmban - Sokoban in an MBR
Some of the simpler levels are missing because they would not fit on the screen in this resolution, so it might make some jumps. I'll look into clearing the buffer between levels.Octocontrabass wrote: ↑Sun Jan 19, 2025 7:36 pm It does seem to be fixed, I was able to play through a few levels without any trouble (aside from the rather steep difficulty curve). I did notice that the input buffer doesn't get flushed between levels, but you can just restart the level if the buffered input messes something up.
How are the animations and audio? Does it work?
Well, if I can find a dump or rom of this BIOS somewhere... might be impossible, though.Octocontrabass wrote: ↑Sun Jan 19, 2025 7:36 pm It's possible, but where will you get a different BIOS that works in a virtual machine?
It's an AMI Hi-Flex BIOS with a core date of July 15th, 1995.
-
- Member
- Posts: 5655
- Joined: Mon Mar 25, 2013 7:01 pm
Re: x86asmban - Sokoban in an MBR
Everything else seems to work fine.
Virtual machines usually aren't close enough to real hardware to satisfy BIOS POST routines. If you still want to give it a shot, it's not too hard to find an "071595-i440fx" BIOS if you know where to look.
I can also send you a copy of the BIOS from my test PC, but I doubt it'll work in any VM.
Re: x86asmban - Sokoban in an MBR
Hi guys, it has been a while since I posted anything. I frequent the forum to read, but lately I seldom post, however, this subject caught my eye.
I have two comments, if I may.
1) (Without sounding rude), I have yet to look at your end result because I don't wish to take the time to build the thing. Granted, it is only NASM and a few files, but have you ever wanted to have a look at someone's OS or other project and there is no binary? Some projects require GCC *and* MAKE *and* a specific version of XYZ, *and* the ABC Library. I don't wish to install of that, let alone most of the time it doesn't build anyway because I downloaded a different version of the ABC Library than was used by the author.
Therefore, and I am sure some people have a different opinion about this, but I think it is nice when the project has an already built binary in the source tree, whether it be a simple MBR.BIN as in your project, or a zipped down CDROM.ISO image, or even a zipped HDD.BIN file.
Maybe I am just lazy, but I don't feel like going through all of the trouble to download and install a build environment just to satisfy curiosity of the project at hand. Therefore, I am asking that you upload a binary image to your github source tree. If not github, maybe somewhere else.
2) One of the recent projects I have been working on is exactly on subject of your last few posts, a BIOS for Bochs and QEMU, which includes a binary within the github source tree :-). It isn't advanced as SeaBIOS (the one QEMU uses), but it has fixed some of the issues with the BIOS Bochs uses. I would appreciate it if you will try it and let me know what you find.
https://github.com/fysnet/i440fx/
Thank you,
Ben
I have two comments, if I may.
1) (Without sounding rude), I have yet to look at your end result because I don't wish to take the time to build the thing. Granted, it is only NASM and a few files, but have you ever wanted to have a look at someone's OS or other project and there is no binary? Some projects require GCC *and* MAKE *and* a specific version of XYZ, *and* the ABC Library. I don't wish to install of that, let alone most of the time it doesn't build anyway because I downloaded a different version of the ABC Library than was used by the author.
Therefore, and I am sure some people have a different opinion about this, but I think it is nice when the project has an already built binary in the source tree, whether it be a simple MBR.BIN as in your project, or a zipped down CDROM.ISO image, or even a zipped HDD.BIN file.
Maybe I am just lazy, but I don't feel like going through all of the trouble to download and install a build environment just to satisfy curiosity of the project at hand. Therefore, I am asking that you upload a binary image to your github source tree. If not github, maybe somewhere else.
2) One of the recent projects I have been working on is exactly on subject of your last few posts, a BIOS for Bochs and QEMU, which includes a binary within the github source tree :-). It isn't advanced as SeaBIOS (the one QEMU uses), but it has fixed some of the issues with the BIOS Bochs uses. I would appreciate it if you will try it and let me know what you find.
https://github.com/fysnet/i440fx/
Thank you,
Ben