Turbo51 Objects

Pascal Compiler for 8051 Microcontrollers

Objects are data structures that merge pascal records and procedures called methods, i.e. data and code together. In order to use objects in Turbo51 you need XDATA memory. The syntax is equivalent to that in Borland Turbo Pascal 7. Turbo51 supports:

  • Inheritance
  • Static and dynamic objects
  • Private fields
  • Constructors and destructors
  • Static, virtual and dynamic methods

Example:

Program OOP;

{$M $0000, $1000, $0000, $1000, 0}

Type
  TLocation = Object
                X, Y : Integer;
                Procedure Init (InitX, InitY: Word);
                Function  GetX: Word;
                Function  GetY: Word;
              end;

  TPoint = Object (TLocation)
             Visible:  ByteBool;
             Procedure Init (InitX, InitY: Word);
             Procedure Show;
             Procedure Hide;
             Function  IsVisible: byteBool;
             Procedure MoveTo (NewX, NewY: Word);
           end;

Const
 clBlack = 0;
 clGreen = 2;

Var Point: TPoint XDATA;

Procedure PutPixel (X, Y: Word; Color: Byte);
begin
// Code to draw pixel
end;

Procedure TLocation.Init (InitX, InitY: Word);
begin
  X := InitX;
  Y := InitY;
end;

Function TLocation.GetX: Word;
begin
  GetX := X;
end;

Function TLocation.GetY: Word;
begin
  GetY := Y;
end;

Procedure TPoint.Init (InitX, InitY: Word);
begin
  TLocation.Init (InitX, InitY);
  Visible := False;
end;

Procedure TPoint.Show;
begin
  Visible := True;
  PutPixel (X, Y, clGreen);
end;

Procedure TPoint.Hide;
begin
  Visible := False;
  PutPixel (X, Y, clBlack);
end;

Function TPoint.IsVisible: ByteBool;
begin
  IsVisible := Visible;
end;

Procedure TPoint.MoveTo (NewX, NewY: Word);
begin
  Hide;
  X := NewX;
  Y := NewY;
  Show;
end;

begin
  Point.Init (100, 50);    // Initial X,Y at 10, 50
  Point.Show;              // APoint turns itself on
  Point.MoveTo (120, 100); // APoint moves to 120, 100
  Point.Hide;              // APoint turns itself off

  With Point do
    begin
      Init (100, 50);    // Initial X, Y at 100, 50
      Show;              // APoint turns itself on
      MoveTo (120, 100); // APoint moves to 120, 100
      Hide;
    end;
end.

And compiled code would look like this:

; Turbo51 version 0.1.3.10, Copyright 2000 - 2011 Igor Funa

$REGISTERBANK (0)

_CODE         SEGMENT  CODE
_DATA         SEGMENT  DATA
_XDATA        SEGMENT  XDATA

              EXTRN    DATA  (XSP)
              EXTRN    DATA  (XBP)
              EXTRN    IDATA (StackStart)
              EXTRN    XDATA (XDATA_StackStart)

              EXTRN    CODE  (sysEnterMethod)
              EXTRN    CODE  (sysLoad_XBP_Plus_DPTR_To_DPTR)
              EXTRN    CODE  (sysLoadSelfAddress)
              EXTRN    CODE  (sysLoadSelfAddressPlus_R1)
              EXTRN    CODE  (sysLeaveProc)
              EXTRN    CODE  (sysLoadSelfAddressTo_R3R2)

; Program OOP;
; 
; {$M $0000, $1000, $0000, $1000, 0}
; 
; Type
;   TLocation = Object
;                 X, Y : Integer;
;                 Procedure Init (InitX, InitY: Word);
;                 Function  GetX: Word;
;                 Function  GetY: Word;
;               end;
; 
;   TPoint = Object (TLocation)
;              Visible:  ByteBool;
;              Procedure Init (InitX, InitY: Word);
;              Procedure Show;
;              Procedure Hide;
;              Function  IsVisible: byteBool;
;              Procedure MoveTo (NewX, NewY: Word);
;            end;
; 
; Const

RSEG _CONST

;  clBlack = 0;
;  clGreen = 2;
; 
; Var Point: TPoint XDATA;

RSEG _XDATA

Point:                          DS       5

; 
; Procedure PutPixel (X, Y: Word; Color: Byte);

RSEG _DATA

X:                              DS       2
Y:                              DS       2
Color:                          DS       1


RSEG _CODE

USING 0

PutPixel:
; begin
; // Code to draw pixel
; end;
              RET

; 
; Procedure TLocation.Init (InitX, InitY: Word);

TLocation_Init:
; begin
              MOV       R0, #0
              LCALL     sysEnterMethod

;   X := InitX;
              MOV       DPTR, #-6
              LCALL     sysLoad_XBP_Plus_DPTR_To_DPTR
              MOVX      A, @DPTR
              MOV       R2, A
              INC       DPTR
              MOVX      A, @DPTR
              MOV       R3, A
              LCALL     sysLoadSelfAddress
              MOV       A, R2
              MOVX      @DPTR, A
              INC       DPTR
              MOV       A, R3
              MOVX      @DPTR, A

;   Y := InitY;
              MOV       DPTR, #-4
              LCALL     sysLoad_XBP_Plus_DPTR_To_DPTR
              MOVX      A, @DPTR
              MOV       R2, A
              INC       DPTR
              MOVX      A, @DPTR
              MOV       R3, A
              MOV       R1, #2
              LCALL     sysLoadSelfAddressPlus_R1
              MOV       A, R2
              MOVX      @DPTR, A
              INC       DPTR
              MOV       A, R3
              MOVX      @DPTR, A

; end;
              MOV       DPTR, #-6
              LJMP      sysLeaveProc

; 
; Function TLocation.GetX: Word;
; begin
;   GetX := X;
; end;
; 
; Function TLocation.GetY: Word;
; begin
;   GetY := Y;
; end;
; 
; Procedure TPoint.Init (InitX, InitY: Word);

TPoint_Init:
; begin
              MOV       R0, #0
              LCALL     sysEnterMethod

;   TLocation.Init (InitX, InitY);
              MOV       DPTR, #-6
              LCALL     sysLoad_XBP_Plus_DPTR_To_DPTR
              MOVX      A, @DPTR
              MOV       R2, A
              INC       DPTR
              MOVX      A, @DPTR
              MOV       R3, A
              MOV       DPL, XSP
              MOV       DPH, XSP+1
              MOV       A, R2
              MOVX      @DPTR, A
              INC       DPTR
              MOV       A, R3
              MOVX      @DPTR, A
              INC       DPTR
              MOV       XSP+1, DPH
              MOV       XSP, DPL
              MOV       DPTR, #-4
              LCALL     sysLoad_XBP_Plus_DPTR_To_DPTR
              MOVX      A, @DPTR
              MOV       R2, A
              INC       DPTR
              MOVX      A, @DPTR
              MOV       R3, A
              MOV       DPL, XSP
              MOV       DPH, XSP+1
              MOV       A, R2
              MOVX      @DPTR, A
              INC       DPTR
              MOV       A, R3
              MOVX      @DPTR, A
              INC       DPTR
              MOV       XSP+1, DPH
              MOV       XSP, DPL
              LCALL     sysLoadSelfAddressTo_R3R2
              LCALL     TLocation_Init

;   Visible := False;
              MOV       R1, #4
              LCALL     sysLoadSelfAddressPlus_R1
              CLR       A
              MOVX      @DPTR, A

; end;
              MOV       DPTR, #-6
              LJMP      sysLeaveProc

; 
; Procedure TPoint.Show;

TPoint_Show:
; begin
              MOV       R0, #0
              LCALL     sysEnterMethod

;   Visible := True;
              MOV       R1, #4
              LCALL     sysLoadSelfAddressPlus_R1
              MOV       A, #1
              MOVX      @DPTR, A

;   PutPixel (X, Y, clGreen);
              LCALL     sysLoadSelfAddress
              MOVX      A, @DPTR
              MOV       X, A
              INC       DPTR
              MOVX      A, @DPTR
              MOV       X+1, A
              MOV       R1, #2
              LCALL     sysLoadSelfAddressPlus_R1
              MOVX      A, @DPTR
              MOV       Y, A
              INC       DPTR
              MOVX      A, @DPTR
              MOV       Y+1, A
              MOV       Color, #2
              LCALL     PutPixel

; end;
              MOV       DPTR, #-2
              LJMP      sysLeaveProc

; 
; Procedure TPoint.Hide;

TPoint_Hide:
; begin
              MOV       R0, #0
              LCALL     sysEnterMethod

;   Visible := False;
              MOV       R1, #4
              LCALL     sysLoadSelfAddressPlus_R1
              CLR       A
              MOVX      @DPTR, A

;   PutPixel (X, Y, clBlack);
              LCALL     sysLoadSelfAddress
              MOVX      A, @DPTR
              MOV       X, A
              INC       DPTR
              MOVX      A, @DPTR
              MOV       X+1, A
              MOV       R1, #2
              LCALL     sysLoadSelfAddressPlus_R1
              MOVX      A, @DPTR
              MOV       Y, A
              INC       DPTR
              MOVX      A, @DPTR
              MOV       Y+1, A
              MOV       Color, R0
              LCALL     PutPixel

; end;
              MOV       DPTR, #-2
              LJMP      sysLeaveProc

; 
; Function TPoint.IsVisible: ByteBool;
; begin
;   IsVisible := Visible;
; end;
; 
; Procedure TPoint.MoveTo (NewX, NewY: Word);

TPoint_MoveTo:
; begin
              MOV       R0, #0
              LCALL     sysEnterMethod

;   Hide;
              LCALL     sysLoadSelfAddressTo_R3R2
              LCALL     TPoint_Hide

;   X := NewX;
              MOV       DPTR, #-6
              LCALL     sysLoad_XBP_Plus_DPTR_To_DPTR
              MOVX      A, @DPTR
              MOV       R2, A
              INC       DPTR
              MOVX      A, @DPTR
              MOV       R3, A
              LCALL     sysLoadSelfAddress
              MOV       A, R2
              MOVX      @DPTR, A
              INC       DPTR
              MOV       A, R3
              MOVX      @DPTR, A

;   Y := NewY;
              MOV       DPTR, #-4
              LCALL     sysLoad_XBP_Plus_DPTR_To_DPTR
              MOVX      A, @DPTR
              MOV       R2, A
              INC       DPTR
              MOVX      A, @DPTR
              MOV       R3, A
              MOV       R1, #2
              LCALL     sysLoadSelfAddressPlus_R1
              MOV       A, R2
              MOVX      @DPTR, A
              INC       DPTR
              MOV       A, R3
              MOVX      @DPTR, A

;   Show;
              LCALL     sysLoadSelfAddressTo_R3R2
              LCALL     TPoint_Show

; end;
              MOV       DPTR, #-6
              LJMP      sysLeaveProc


CSEG AT $0000

OOP:
; 
; begin
              MOV       SP, #StackStart-1
              MOV       A, #LOW  (XDATA_StackStart)
              MOV       XBP, A
              MOV       XSP, A
              MOV       A, #HIGH (XDATA_StackStart)
              MOV       XBP+1, A
              MOV       XSP+1, A

;   Point.Init (100, 50);    // Initial X,Y at 10, 50
              MOV       DPL, XSP
              MOV       DPH, XSP+1
              MOV       A, #LOW  ($0064)
              MOVX      @DPTR, A
              INC       DPTR
              MOV       A, #HIGH ($0064)
              MOVX      @DPTR, A
              INC       DPTR
              MOV       XSP+1, DPH
              MOV       XSP, DPL
              MOV       A, #LOW  ($0032)
              MOVX      @DPTR, A
              INC       DPTR
              MOV       A, #HIGH ($0032)
              MOVX      @DPTR, A
              INC       DPTR
              MOV       XSP+1, DPH
              MOV       XSP, DPL
              MOV       R2, #LOW  (Point)
              MOV       R3, #HIGH (Point)
              LCALL     TPoint_Init

;   Point.Show;              // APoint turns itself on
              MOV       R2, #LOW  (Point)
              MOV       R3, #HIGH (Point)
              LCALL     TPoint_Show

;   Point.MoveTo (120, 100); // APoint moves to 120, 100
              MOV       DPL, XSP
              MOV       DPH, XSP+1
              MOV       A, #LOW  ($0078)
              MOVX      @DPTR, A
              INC       DPTR
              MOV       A, #HIGH ($0078)
              MOVX      @DPTR, A
              INC       DPTR
              MOV       XSP+1, DPH
              MOV       XSP, DPL
              MOV       A, #LOW  ($0064)
              MOVX      @DPTR, A
              INC       DPTR
              MOV       A, #HIGH ($0064)
              MOVX      @DPTR, A
              INC       DPTR
              MOV       XSP+1, DPH
              MOV       XSP, DPL
              MOV       R2, #LOW  (Point)
              MOV       R3, #HIGH (Point)
              LCALL     TPoint_MoveTo

;   Point.Hide;              // APoint turns itself off
              MOV       R2, #LOW  (Point)
              MOV       R3, #HIGH (Point)
              LCALL     TPoint_Hide

; 
;   With Point do
;     begin
;       Init (100, 50);    // Initial X, Y at 100, 50
              MOV       DPL, XSP
              MOV       DPH, XSP+1
              MOV       A, #LOW  ($0064)
              MOVX      @DPTR, A
              INC       DPTR
              MOV       A, #HIGH ($0064)
              MOVX      @DPTR, A
              INC       DPTR
              MOV       XSP+1, DPH
              MOV       XSP, DPL
              MOV       A, #LOW  ($0032)
              MOVX      @DPTR, A
              INC       DPTR
              MOV       A, #HIGH ($0032)
              MOVX      @DPTR, A
              INC       DPTR
              MOV       XSP+1, DPH
              MOV       XSP, DPL
              MOV       R2, #LOW  (Point)
              MOV       R3, #HIGH (Point)
              LCALL     TPoint_Init

;       Show;              // APoint turns itself on
              MOV       R2, #LOW  (Point)
              MOV       R3, #HIGH (Point)
              LCALL     TPoint_Show

;       MoveTo (120, 100); // APoint moves to 120, 100
              MOV       DPL, XSP
              MOV       DPH, XSP+1
              MOV       A, #LOW  ($0078)
              MOVX      @DPTR, A
              INC       DPTR
              MOV       A, #HIGH ($0078)
              MOVX      @DPTR, A
              INC       DPTR
              MOV       XSP+1, DPH
              MOV       XSP, DPL
              MOV       A, #LOW  ($0064)
              MOVX      @DPTR, A
              INC       DPTR
              MOV       A, #HIGH ($0064)
              MOVX      @DPTR, A
              INC       DPTR
              MOV       XSP+1, DPH
              MOV       XSP, DPL
              MOV       R2, #LOW  (Point)
              MOV       R3, #HIGH (Point)
              LCALL     TPoint_MoveTo

;       Hide;
              MOV       R2, #LOW  (Point)
              MOV       R3, #HIGH (Point)
              LCALL     TPoint_Hide

;     end;
; end.
L_00CF:
              SJMP      L_00CF

RSEG _CONST


              END

Copyright © 2024 Igor Funa. All Rights Reserved. Terms, Conditions and Privacy policy