1B MicroScript Thorough Primer, Final No.

Toshihisa Muto

Personal Media Corporation


This installment will, for the present, become the final number in this series in which we have dealt with the 1B MicroScript programming language on BTRON from various angles. Accordingly, I will briefly do a summary of things up to here.

Over 10 installments up to here, I have explained the "1B MicroScript" programming language that runs on BTRON. At the beginning of the series, I mixed together an introduction from the application side as to what what types of things can be done using this language together with concrete programming examples. Therefore, on this occasion, I would like to review once again, in the sense of practice, the characteristic mechanisms that 1B MicroScript possesses.

Event Driven

The basic structure of 1B MicroScript operation is event driven. In other words, 1B MicroScript is normally in a state where it is waiting for an event from a user (input) or a signal event from an external device. When an event of those types occurs, MicroScript carries out the specified processing, and when that finishes, MicroScript returns again to an event wait state.

In concrete terms, MicroScript in the beginning first executes between the PROLOGUE statement and the END statement in the program. Normally, various types of initial settings are carried out here. And then, MicroScript enters a wait state. For example, one thinks of a program in which every time the user clicks a button on the screen that says "Stop," a beep sound rings out. In this case, we describe the BEEP statement in the place enclosed by an ACTION statement and an END statement. We write the event type (here, "CLICK") and the name (here, "Stop Button") of the figure (segment) the becomes the object that generates the event (Program 1).

Program 1
Execution of Initial Processing and Processing When the Button Has Been Clicked
#
# Complete MicroScript Program Example (1)[TN1]
#
ACTION Stop_Button_Pressed CLICK Stop_Button
 # Ring out a beep sound if stop button is clicked.
 BEEP
END
#
# A MicroScript program first executes between the PROLOGUE statement and
# the END statement.
#
PROLOGUE
 # Show "Stop Button" on the screen
 SCENE Stop_Button
 # Enter wait when execution of execution of program inside PROLOGUE~END

Additionally, as an instruction that carries out waiting, there is the RSWAIT statement for the purpose of waiting the input of the RS-232C port. For example, a program that would ring out a beep sound every time two-byte data in the order of 0x0d → 0x0a came in would be in the form of Program 2.

Program 2
Execution of Initial Processing and Processing When There Has Been an RS-232C Input
#
# Complete MicroScript Program Example (2)
#
DEFINE PORT 0 # RS port #0
DEFINE MODE 0018 # No flow control, no parity check
        # Stop bit 1, data length 8
DEFINE SPEED 4800 # Transmission at 4800 bps

ACTION RS232C_Processing
 # Ring out beep sound when the two bytes 0x0d 0x0a have come in.
 REPEAT
  RSWAIT PORT 0x0d 0x0a
  BEEP
 ENDREPEAT
END
#
# A MicroScript program first executes between the PROLOGUE statement and
# the END statement.
#
PROLOGUE
 RSINIT PORT MODE SPEED
 EXECUTE RS232C_Processing
END

When doing programming with MicroScript, adjusting the events that come in from the outside and firmly grasping beforehand the event driven structure are essential. In actual programming work itself, there are also a lot of parts where it doesn't matter even if it is carried out without being conscious of this type of event driven structure, but as for the basics, they lie in an event driven structure throughout.

Multiple Threads

In actual programming, there are cases where many types of events come in with various timings (asynchronously). Accordingly, what becomes important are threads. A thread is a processing unit of script that runs in parallel.

When MicroScript is executing between the PROLOGUE and END statements in the beginning, the number of threads is one. When its execution finishes and MicroScript enters the wait state, the thread disappears. Threads increase in number in any of the following cases.

(1) A procedure is specified with the EXECUTE statement

Example: Program 2

(2) An event such as CLICK launches and a procedure starts up

Example: Program 1

When all the procedures that have started up end, their threads disappear. In MicroScript it is possible to make two or more threads exist simultaneously (multiple threads).

Thread Creation Based on the EXECUTE Statement

For example, if we write

EXECUTE Move_Ball Move_Bar

in addition to the current thread, the procedures Move_Ball and Move_Bar will start up in parallel simultaneously, and there will become a state in which a total of three threads are running simultaneously (Fig. 1).

Figure 1. Thread creation based on EXECUTE statement

The CALL statement exists as something to do so-called subroutine calls. With the CALL statement, just the procedure that executes changes, nothing changes in regard to the thread (Fig. 2).

Figure 2. CALL and EXECUTE statements

Thread Creation Based on a CLICK Event, Etc.

A thread is created even by means of an event such as CLICK occurring and a procedure being executed. For example, I show in Fig. 3 the states when a "Stop Button" and a "Reset Button" are displayed on the screen and those have been clicked.

Figure 3. Thread creation based on events

If the reset button is clicked prior to the processing from when the stop button was clicked terminates, it will come about that the two click processes will run simultaneously in parallel.

Relationship with BTRON Multitasking

BTRON is a multitask operating system. In that case, what sort of relationship exists between multitasking and multi-threading? A thread, to the last, is a processing unit inside one MicroScript processing system. And then, two or more MicroScript processing systems (tasks) are started up using the BTRON multitask function. In concrete terms, multiple MicroScripts are started up based on a user double clicking on MicroScript virtual objects, or through the use of VOPEN statements from inside script programs.

In other words, it comes down to a relationship in which large parallel execution units called "tasks" exist, and inside each task there exist parallel execution units called "threads."

Functions that Support Parallel Processing

MicroScript is equipped with several functions in support of parallel processing. I have briefly summarized them in Table 1.

Table 1 Various Functions that Support Parallel Processing
VOPEN [<virtual object> . . . ] task creation
VWAIT [<virtual object> . . . ] [: <timeout expression>] waiting for the task to terminate
VCLOSE [<virtual object> . . . ] forcibly terminating the task
 
EXECUTE <procedure name> . . . thread creation
SUSPEND [<procedure name> . . . ] waiting for the thread to terminate
TERMINATE [<procedure name> . . . ] forcibly extinguishing the thread
BREAK [<procedure name> . . . ] forcible release of the wait state
 
RSWAIT <port expression> <receive data expression> . . . [: <timeout expression>] wait for RS-232C data input
 
WAIT <conditional expression> [: <timeout expression>] wait until <conditional expression> is established
 
<procedure name>.S obtain the execution state of <procedure name>
0: not executing 1: executing 2: wait state
. . . : recursion possible [ ] : omission possible

Variables

Variables that can be handled with MicroScript can be classified into the four of Table 2 based on the range they are effective within and the time they are saved.

Table 2 MicroScript Variables

Effective Range

Period Saved
Local Variable Only inside that procedure (thread) Until that procedure (thread) terminates
Global Variable All procedures (threads) belonging to that window (task) Until that window (task) terminates
Shared Variable All procedures (threads) running on the 1B system overall Until the 1B system electric power source is cut off
Saved Variable All procedures (threads) belonging to that task Even if the electric power source is cut off, if you open the window once again, that value remains

Summary

I have summarized in Table 3 and Fig. 4 the relationships of the above MicroScript parallel execution units and variables.

Table 3 A Comparison of Threads and Tasks

Thread

Task
Parallel Startup Unit Procedure Script
Description Method ACTION~END One script program
Cue for Starting Up
EXECUTE statement
Segment CLICK, etc.
VOPEN statement
Virtual object startup
Realization Mechanism Language task BTRON OS kernel function

Figure 4. Relationship of each variable with scripts/threads

In Closing

In this article, in the sense of a summary, I have explained 1B MicroScript by concentrating on "event driven" and "multitasking." That programs can possess even smaller processing units called "threads" inside "tasks" in order to sufficiently make use of BTRON's multitasking is its forte.

For example, there are a lot of cases in which systems of the type that have asynchronous event inputs from the outside have become complex with traditional programing techniques. However, if we cleverly divide among and use the two parallel execution units (tasks and threads) that 1B MicroScript possesses, we can do the programming very neatly (Vols. 25, 26). You might feel that parallel programming is difficult while you are not used to it. However, if you can neatly design the basic mechanism in the beginning, you can simplify the programming, and even maintenance and modification work become easy.

On other personal computers also, visually oriented programming languages/environments along the lines of 1B MicroScript are provided. However, 1B MicroScript is a platform for the multitasking operating system BTRON, and that it is capable of true parallel programming is its great forte. Environments in which you can realize true parallel programming to this extent on top of a personal computer are extremely rare. 1B MicroScript is, of course, suitable for practical use, but it is also so as an environment in which one can easily practice and/or experiment with parallel programming.

Finally, I have summarized in Table 4 the contents that I have dealt with in this series. Thank you for following along with me over this long time.

Table 4 "1B MicroScript Thorough Primer"

No. 1

Vol. 22 (93/8) Actualities of programming

No. 2

Vol. 23 ((93/10) Device control based on an infrared remote controller

No. 3

Vol. 25 (94/2) Shooting game (parallel startup based on EXECUTE)

No. 4

Vol. 26 (94/4) Competitive shooting game (parallel startup based on EXECUTE and VOPEN)

No. 5

Vol. 27 (94/6) Parts library 1: selectors, switches

No. 6

Vol. 28 (94/8) Parts library 2: volume control

No. 7

Vol. 29 (94/10) Parts library 3: numerical values box

No. 8

Vol. 30 (94/12) Presentation software

No. 9

Vol. 31 (95/2) Parts library 4: heading tabs

No. 10

Vol. 32 (95/4) Parts library 5: pop-up selector

No. 11

Vol. 33 (95/6) Summary

____________________

[TN1] As noted elsewhere in translator's footnotes appended to this series, which is 13 years old, a MicroScript program written today has to begin with VERSION3. Also, this isn't a complete program in the sense that it can be copied, pasted, and executed, rather it is the complete framework of a program.


The above article on MicroScript appeared on pages 58-61 in Vol. 33 of TRONWARE. It was translated and loaded onto this Web page with the permission of Personal Media Corporation.

Copyright © 1995 Personal Media Corporation

Copyright © 2008 Sakamura Laboratory, University Museum, University of Tokyo