Skip to content

Fortran Library #11

@ivan-pi

Description

@ivan-pi

When posting to Fortran Discourse I mentioned the possibility of creating a small Fortran-like language and runtime library: https://fortran-lang.discourse.group/t/fortran-chip-8-interpreter/6772/5?u=ivanpribec

Programmers could develop/test in Fortran, and then compile to CHIP-8.

The runtime library can be shared with emulator:

  • drawing sprites to the screen
  • clearing the screen
  • setting the sound and delay timers
  • checking for key presses
  • converting an integer to a binary-coded decimal

Since Fortran nowadays is usually hosted on top of the C platform, it is possible to "hack" the Fortran initialization to inject additional setup procedures (e.g. turning on a screen and connecting the keyboard): https://fortran-lang.discourse.group/t/initializing-global-module-variable-object/5899/6?u=ivanpribec

We could also have a F77 like library of external routines:

C runtime routines
      external draw, clear, sound, delay, press, bcd, irand

The following target can be used to play with the idea:

C JUMPING X AND O
C
C A CHIP8 PROGRAM, ORIGINALLY BY HARRY KLEINBERG
C                                RCA CORPORATION, 1978
C
C REWRITTEN IN F77, IN 2023, BY IVAN PRIBEC
C
      PROGRAM JUMPING_X_AND_O
      INTEGER*1 XS(5),OS(5),BS(6)
C SPRITE DATA
      DATA XS,OS,BS/
     *  Z'88',Z'50',Z'20',Z'50',Z'88',
     *  Z'F8',Z'88',Z'88',Z'88',Z'F8',
     *  6*Z'FC'/
C SUBROUTINES USED
      EXTERNAL DRAW, JUMP
C START PROGRAM
      CALL DRAW(48,4,BS,6)
   10 CALL JUMP(XS,5)
      CALL JUMP(OS,5)
      GO TO 10
      END
C
C RANDOMLY DRAW A PATTERN, UNTIL IT OVERLAPS WITH THE BLOCK
C
      SUBROUTINE JUMP(PTRN,N)
      INTEGER*1 PTRN(*), N
      INTEGER*1 X, Y, K
      LOGICAL*1 OVRLPS
      INTEGER*1 IRAND
      EXTERNAL DRAW, DELAY, TIMER, IRAND
      X = 30
      Y = 13
C START JUMPING AROUND
   10 CALL DRAW(X,Y,PTRN,N,OVRLPS)
C
C COUNT DOWN DELAY TIMER
C DELAY TIMER CAN BE FOUND AT UNIT=33
C
      WRITE (33) 12
   20 READ (33) K
      IF (K > 0) GO TO 20
C IF PATTERN OVERLAPS WITH BLOCK, WE SWITCH
      IF (OVRLPS) GO TO 30
C
C ERASE PATTERN, SO WE CAN REDRAW AT NEW COORDINATES
C
      CALL DRAW(X,Y,PTRN,N)
      X = IRAND(Z'3F') ! 6-BIT RANDOM NUMBER [0, 63]
      Y = IRAND(Z'1F') ! 5-BIT RANDOM NUMBER [0, 31]
      GO TO 10
C
C ERASE PATTERN AND RETURN
C
   30 CALL DRAW(X,Y,PTRN,N)
      RETURN
      END

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions