Post Reply 
voyc: Compile CommodoreBASIC to HP 15-C
06-11-2019, 03:28 PM
Post: #1
voyc: Compile CommodoreBASIC to HP 15-C
I wrote a compiler that compiles programs written in CommodoreBASIC (C64 BASIC) to HP 15-C without changes. See https://gitlab.com/michaelzinn/voyc

The compiler is incomplete (no support for arrays or functions yet), but it's enough to compile some games or other programs.

Output is meant to be user friendly, so it outputs the program both in a plain format that works well with the SwissMicros assembler (to produce an image to send to the calculator over serial) as well as a (slightly buggy) side by side view of the BASIC program and the calculator code, like this:

Code:

270 IF Z=11 THEN 860.....................................................154 6
                                                                         155 6
                                                                         156 5
                                                                         157 CHS
                                                                         158 STO I
                                                                         159 1
                                                                         160 1
                                                                         161 RCL .4
                                                                         162 TEST 5
                                                                         163 GTO I
310 C=INT(10*RND(1)): Y=C+17.............................................164 1
                                                                         165 0
                                                                         166 RAN#
                                                                         167 *
                                                                         168 INT
                                                                         169 STO 1
                                                                         170 RCL 1
                                                                         171 1
                                                                         172 7
                                                                         173 +
                                                                         174 STO .3

So far, I managed to play this game on my calculator: http://vintage-basic.net/bcg/hammurabi.bas

Using the compiler is currently a bit tricky (you need to install Idris and run it from the command line), the long term plan is to have it in JavaScript so that you can just download an HTML file with a text input box where you paste your basic code and click a "compile" button or something like that.
Find all posts by this user
Quote this message in a reply
06-16-2019, 06:23 PM
Post: #2
RE: voyc: Compile CommodoreBASIC to HP 15-C
New Release! The compiler now supports the ON statement, which allows you to GOTO/GOSUB to different lines depending on what's in a variable (1 goes to the first line number, 2 to the second and so on).

Example:
This program displays 123 and then allows the user to enter 1, 2 or 3 to get 111, 222 or 333 in response.

Code:

0 REM Enter 1 or 2 or 3 to get 111 or 222 or 333 as output
10 PRINT 123
20 INPUT X
30 ON X GOSUB 111, 222, 333
40 GOTO 10
111 PRINT 111
112 RETURN
222 PRINT 222
223 RETURN
333 PRINT 333
334 RETURN

Compiled this looks like this (It's very bloated, compiler optimizations will come later):

Code:

001  LBL 0        | 42,21, 0
002  R/S          |       31
003  CF 8         | 43, 5, 8
004  RTN          |   43  32
005  LBL 1        | 42,21, 1
006  CHS          |       16
007  STO I        |   44  25
008  LBL 2        | 42,21, 2
009  GTO I        |   22  25
010  LBL 3        | 42,21, 3
011  CHS          |       16
012  x<>y         |       34
013  1            |        1
014  -            |       30
015  TEST 0       | 43,30, 0
016  RTN          |   43  32
017  R_down       |       33
018  STO I        |   44  25
019  R_up         |   43  33
020  RTN          |   43  32
021  LBL B        | 42,21,12
022  1            |        1
023  2            |        2
024  3            |        3
025  GSB 0        |    32  0
026  SF 9         | 43, 4, 9
027  R/S          |       31
028  CF 9         | 43, 5, 9
029  STO 0        |    44  0
030  0            |        0
031  STO I        |   44  25
032  RCL 0        |    45  0
033  INT          |   43  44
034  0            |        0
035  5            |        5
036  3            |        3
037  GSB 3        |    32  3
038  0            |        0
039  5            |        5
040  8            |        8
041  GSB 3        |    32  3
042  0            |        0
043  6            |        6
044  3            |        3
045  GSB 3        |    32  3
046  RCL I        |   45  25
047  TEST 0       | 43,30, 0
048  GSB 2        |    32  2
049  0            |        0
050  2            |        2
051  2            |        2
052  GTO 1        |    22  1
053  1            |        1
054  1            |        1
055  1            |        1
056  GSB 0        |    32  0
057  RTN          |   43  32
058  2            |        2
059  2            |        2
060  2            |        2
061  GSB 0        |    32  0
062  RTN          |   43  32
063  3            |        3
064  3            |        3
065  3            |        3
066  GSB 0        |    32  0
067  RTN          |   43  32

Here's the annotated version that shows how it gets compiled in more detail:

Code:

                                                            1 LBL 0
                                                            2 R/S
                                                            3 CF 8
                                                            4 RTN
                                                            5 LBL 1
                                                            6 CHS
                                                            7 STO I
                                                            8 LBL 2
                                                            9 GTO I
                                                            10 LBL 3
                                                            11 CHS
                                                            12 x<>y
                                                            13 1
                                                            14 -
                                                            15 TEST 0
                                                            16 RTN
                                                            17 R_down
                                                            18 STO I
                                                            19 R_up
                                                            20 RTN
                                                            21 LBL B
10 PRINT 123................................................22 1
                                                            23 2
                                                            24 3
                                                            25 GSB 0
20 INPUT X..................................................26 SF 9
                                                            27 R/S
                                                            28 CF 9
                                                            29 STO 0
30 ON X GOSUB 111, 222, 333.................................30 0
                                                            31 STO I
                                                            32 RCL 0
                                                            33 INT
                                                            34 0
                                                            35 5
                                                            36 3
                                                            37 GSB 3
                                                            38 0
                                                            39 5
                                                            40 8
                                                            41 GSB 3
                                                            42 0
                                                            43 6
                                                            44 3
                                                            45 GSB 3
                                                            46 RCL_I
                                                            47 TEST 0
                                                            48 GSB 2
40 GOTO 10..................................................49 0
                                                            50 2
                                                            51 2
                                                            52 GTO 1
111 PRINT 111...............................................53 1
                                                            54 1
                                                            55 1
                                                            56 GSB 0
112 RETURN..................................................57 RTN
222 PRINT 222...............................................58 2
                                                            59 2
                                                            60 2
                                                            61 GSB 0
223 RETURN..................................................62 RTN
333 PRINT 333...............................................63 3
                                                            64 3
                                                            65 3
                                                            66 GSB 0
334 RETURN..................................................67 RTN

The ON code is very tricky and I'm not sure if it's optimal. Feel free to suggest improvements!
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)