
MSX Turbo Pascal Column by Stefano Roperto (part 1)
A look from above
This is intended as an introductory course to programming in Turbo Pascal language in the MSX environment. It is not necessary, but it certainly makes the task easier, to know some programming language. However, it is essential to have access to Borland's Turbo Pascal 3.0 and an MSX machine, or an emulator, so as to be able to put the topics covered into practice. The course will be structured as follows:
1. Introduction to Turbo Pascal 3.0: brief presentation of the main menu of Borland Turbo Pascal 3.0
2. Introduction to the Turbo Pascal language: we will see the syntax of the language and the structure of a program.
3. Data Types: Turbo Pascal is quite rigid in dealing with primitive data types, but very flexible in dealing with programmer-defined data types. 4. Instruction Set: Like any programming language, Turbo Pascal has its own instruction set, as well as a set of predefined identifiers. 5. Loops and control structures: in Turbo Pascal we find more complete iterative structures and control instructions than in MSX BASIC.
6. Procedures and functions. Turbo Pascal allows you to divide a program into smaller parts by writing procedures and functions
7. Files. We will see how to operate on various file types.
8. External libraries: one of the great versatility of Turbo Pascal is being able to produce external libraries to include in your programs also, but not only, to add functions not foreseen by the language.
9. Bibliography: for those wishing to delve deeper into the topic.
INTRODUCTION TO TURBO PASCAL
Chapter 1
MSX is not just BASIC. Although the first approach with an MSX system is BASIC, and later we will return to the importance that Microsoft BASIC had in the youth training of many people who then actually found their way in the world of IT, there are other programming environments with which it is possible to produce software for all MSX systems. Compilers can be found for different languages: COBOL, standard C, Turbo PASCAL, to name the best known, and this thanks to the fact that the MSX DOS operating system is compatible with the Digital Research CP/M operating system and is therefore 8-bit programs produced for that environment can be executed in the MSX Dos environment. In addition to the aforementioned compilers, we can find, for example, programs such as Dbase II by Ashton Tate or Wordstar by MicroPro which made the MSX system professional in the 80s of the last century. Naturally, since it was software designed for a system like CP/M which also ran on machines completely different from MSX, this did not take advantage of the more advanced features of the system such as high resolution graphics or three channel sound, typical of video games, but due to the intrinsic nature of those programs, this was not a problem. The situation is different for programming environments. MSX BASIC, equipped with everything needed to exploit the machine's hardware (graphic instructions, sound production, memory management and so on), certainly represents the simplest way for the production of software, but the limits Objectives linked to the fact that it is an interpreted language do not make it ideal for applications that make heavy use of graphics, such as video games, unless you rely on machine language. Using a language other than basic, such as Borland Turbo Pascal 3.0, poses some fundamental advantages in my opinion. The source code is compiled and runs directly under MSX DOS, this way the basic interpreter is not necessary and therefore there is more memory available for the programs.
The biggest problem regarding the use of a programming environment other than basic lies in the fact that to exploit the characteristics of the hardware it is necessary to use external libraries that provide the necessary extensions. Fortunately, turbo pascal supports external libraries and this allows you to overcome this problem. Various libraries can be found online which provide the most common graphic instructions such as the definition of sprites, the writing and reading of data in the VRAM, the management of the various video modes or access to the routines resident in the ROM, the instructions for programming the PSG, for access to peripherals such as joysticks, printers, modems, floppy disks, etc. Furthermore, no one forbids you to independently write libraries for any utility you want to implement. And this is another of the features of turbo pascal that make it a truly functional environment. Later we will see how this can be done, but now, what I want to make clear is that it is possible to program in a structured way in order to make programs more elegant, functional, with parts that can be easily reused in other programs and easy to maintain. This requires a little effort at the beginning (if you have never used a structured language) but it can give great satisfaction later. Now let's take a closer look at the programming environment. Once you have entered the Msx Dos environment, just insert the disk containing the Turbo Pascal and all the other support files and launch the program by writing TURBO and pressing enter. During loading the program asks if we also want to import the file with the error messages. In the debugging phase it is very useful, but it costs a few kilobytes of memory. Once we have made our choice (Y or N), the screen in figure 1 will appear.

Fig.1 – Main menu
You will notice that some letters are capitalized, which means that just pressing that letter activates that menu item. For example, by pressing the "D" of Dir, we will display the list of files of the current drive, which we can change by pressing the "L" of Logged drive, and so on "E" to edit the source file or to load or create one a new one. By pressing “O” we can access the compiler options which provide for compilation in memory, on an executable file with the .com extension or with the chn extension (which we will see later). Of course, the best thing is to try and see the various options in action.
So why start studying another programming language, when basic Msx, which was born as a language dedicated to the machine, already does everything we need? Meanwhile, learning another programming language is always an enrichment of one's cultural background, furthermore Pascal is based on a different paradigm than basic. Basic is a so-called imperative language, while Pascal falls into the group of structured languages. You will see that it is never really necessary to use the GOTO instruction (which is notoriously the enemy of program readability), that you can divide the program into many smaller structures, procedures and functions (we will see some examples shortly) that it is also possible , as we said before, write libraries of external functions to be incorporated into the program which, being external, if they are well written, can be used in any other program. For example, if we program a library that contains the logic to scroll the screen from bottom to top and vice versa, this library can be used with any other program that needs to scroll up or down , without having to rewrite the whole code every time, so we will only have to deal with the graphic aspect, without worrying about the routine that manages the scroll, which perhaps we wrote a year earlier for another program. I currently use Turbo Pascal 3.0, which can be downloaded from the File Hunter archive, through the famous Blue Msx emulator, since I don't have a real machine, while for editing I use Windows notepad, saving in
a folder that I then load as if it were a disk. The method I use is a bit cumbersome, but effective: first of all, after writing the program, I save it in the folder that I will load as a disk image in the emulator. In drive B of the emulator I load a disk image, a file with the .Dsk extension, because by using a folder as disk, Blue Msx does not save the files, which will therefore have to be saved on drive B for subsequent modifications via Windows Notepad . To tell the truth, before you can edit the file saved on drive B, you need to extract it using the Disk manager program inside the folder we use as disk. Once editing is finished, you need to remove the folder from drive A and reload it so that it updates. You could use the .Dsk file directly, but an extra step would be needed when saving
I said it was a bit cumbersome, but that's really harder said than done. In any case, programming in Pascal in itself is not difficult, it's about learning a new syntax and some new keywords and, as with all new things, trying, experimenting, making mistakes. Good work!


0 Comments