OS - OS for Spectrum: Multitasking, Dispatcher, Interrupt Controller and Timer, Clearboard.

ZX Time #12
 Multitasking             
 -----------------           
 Nikolay Dvornik (KilleRam/KCS)     
                                        
 General thoughts              
 ------------              
                                        
 Again I’m talking about probably everyone
everyone is tired of the topic of multitasking on
SPeCcy.  I want to share with you new
developments in this area. I'm glad that
you would like one person to share it with me
concept, namely - VITAMIN from Coders
Academy (editor's note - his letter at the end
this section).  And I want to talk
about the task manager. Firstly, this pro-
the gram should hang on interruptions -
this is clear.  Use IM1 andIM2
there is no point, since for most pro-
gram they are simply necessary(despite
the fact that you can hang music in the system
as a separate, background task). Besides
but if DI, then we have a complete shutdown
systems.  Therefore, the use ofNMIis not
inevitably as well as a new timer for them
(for example 0.1ms), but more on that later.
It is also necessary to “suspend” the re-
hyster on #fd  to read it, since
tracking which page was on
program at the time of interruption arrival
practically impossible (theoretically)
possible, but with some restrictions:
reading a group of bytes at the start of operation
dispatcher, and then search for them, switching
all pages one by one. It is clear that if
found this on some page (left)
group, then fatal eggog).                
                                        
 Manager               
 -----------              
                                        
Dispatcher-program services for
dacha Her role is task manager.          
                                        
 Currently the following are implemented
commands:                               
                                        
#00, #number of switched on# - stop the
tabernacles, without preserving it, transition to
next one.                              
#01, #included number# - the same, but with
preservation.                            
#02, #task number# - hang on the background
the specified task.                        
#03, #task number# - stop the task.
#04, #task number# - cancel the task.     
#05 - cancel all tasks.                 
#06 - stop everything.                   
                                        
 Moreover, the size of the controller for this
moment - 101Sbytes. It uses two
boundaries :-( memory (#d1,#d2).Implemented
while cooperative multitasking.  B
one stores the saved registers,
interrupt status (di, ei, im1, im2),
blocks of #20 bytes for each task.
The second stores: task names, which
pages (logical).                  
                                        
 Occupies ee coxpoheue in HIMEM (for now
max-8) and passivity/activity flag. B
the same page stores a table of its own
baud memory pages (#00 - free,
#FF - occupied) and the selection is as follows (for now
Lomovskaya, made for testing):         
                                        
 ld de,adress                      
 ld hl,#f000                       
 ld c,8                             
page ld b,0                             
po ld a,(hl)      ог а                               
     inc l                              
     djnz ро                            
     ld а,l      ;номер страницы        
     ld (de),а                          
     inc de                             
     dec c                              
     jp nz,page                         
                                        
   Просто и реально:-)                  
   Кстати,  тип  прерываний определяется
следующим  образом:  по  адресу #0038, в
кэше (для  экспериментов просто forever)
nponucyem программу:                    
                                        
     ld а,#ff                           
ld (adtab),a                      
 ret                               
                                        
 and in the manager after saving the
ructpob, stack, etc., just put     
                                        
ei                                 
 halt                              
 di                                 
                                        
 and that's it :-), if im1 then in adtab - #ff,
if im2 - #00.                         
                                        
 Also, the dispatcher uses 5 bytes of OS
new memory for the resident (in the screen -
#4000):                                 
                                        
 in а,(#7b)                         
jp prog                          
                                        
 The address space is also used
property #8000-#c000, as an intermediate buffer
a ferr for turning pages, of course -
Xia, after saving DOWN-memory.       
                                        
 Interrupt controller, timer      
 -----------------------------     
                                        
 Ideally, you can hang a con-
interrupt troller NMI with full-fledged data
ymer (for example, i8053), I’ll explain why.
For example, one task step is executed
less (or more) than in one interruption
change NMI (0.1ms), which leads to
stagnation of others, (inhibition of this)
dacha, the solution is this: before starting the program,
The dispatcher resets the timer, and in
gram (only system ones are possible, the highest
highest priority), after execution of one
step is recorded in a special table
timer value, and next time
this value is written to the register
periodinterrupt before starting the program
we.  It is clear that the program does not always eat
same number of clock cycles, but the gain is
det.                                    
                                        
 Clipboard               
 -----------              
                                        
 All operations for sending data
I think it is advisable to carry out through special
cial kernel program.  What will exclude
those insanities that were described DWT above (not
as an insult :-) At the same time, this program is preserved
takes up main memory and uses
#6000-#c000 as a buffer. It is clear thatclip-
board - just a clipboard between tasks
data and its speed is not important
pial.  The local buffer must be
Enter in the address field of the task. Tasks
will have a unified system of variables. 
                                        
 That's all. :)             
                                        
 - --                  
                                        
 Comments on KilleRam's article:   
 ----------------------------------  
 Denis Tokarczuk             
                                        
 Let's start with the fact that "hibernation"
you don't need anything on hardwareSpectrum.
Firstly, this is unlikely to be useful
vatel.  Secondly, it seems to me that you...
calls for another wave of incompatibility,
which, you know, is undesirable.      
                                        
 Next. I don’t understand why it’s needed
determining which page is included
at the moment?!? Essentially, this is not necessary
for the new system, but forTRDOS. After all,
the new OS memory management will be
swearing standard procedures, so
any switching must not be "gr-
would be", but "through the system", that is, sanctions
onated.  Investigation - active country
It will always be known and maintained
in OS variables.   About the task manager. I saw this program
ramma at work and it affected me
although not shocking, but in general, painful
What an impression. It works a bit buggy, but
switches tasks (cooperative multi-
task-oriented - worked “together” ACEO.69,
TRMSHOBETA, PKUNZIP, etc.).  Must
say that1015byte is not a value
critical, since the code is still damp and
should lend itself quite well to opti-
mization.                                
                                        
 About clipboard'e.  I will not repeat
"those insanities" (as KilleRam put it),
which are given above.  However, I must
note that the speed in this aspectOS
although not critical, but sensible use
computer resources must be
maximum.                           
                                        
 - - -  Well, at the end of today's "Topics >OS<"
I would like to give an excerpt that is quite in-
Interesting letter fromVitamin,concerning
yes OS.                                 
                                        
 V:"...An article about an OS on spec
in ZXTime#11 made me think about
this topic, which sparked a few thoughts
about this.                        
                                        
 Regarding the system with dual memory.
Quite a complex alteration plus - where
is the ROM gone?  What about switching to another
these processors... I doubt it. This and but-
new command system and new architecture
ra..."                                  
                                        
 DWT: Honestly, I am completely and completely
I completely agree withVitamin. This is
It is very difficult and the implementation of such a system
generates not only “iron” interference
tion, but also fundamental changes in
structure and constitution Spectrumas
cars. We get with nothing hecobmectu-
my "horror"...                       
                                        
 V: "Taming" NMI  - not so much
difficult. This is what I mean. Option many
task problems, where programs contain only
to short transitions and she will change a hundred times
rushes through memory - waste of processor -
a long time..."                       
                                        
 DWT: In my opinion, stop interfering -
Xia in the iron structure of Spectrum.How
It seems to me that installing a new OS should
be limited exclusively to one
replacement of ROM, without any intervention
into iron. As for short transitions
Dov, it really is pointless
and it is labor-intensive when implementing programs.
That is, it’s not only a waste of CPU
time, but also man-hours.           
                                        
 V:"...It's better to just highlight the program
your memory section and let it work there
nerds. Let the last ones be 16k allocated to
tasks, then the penultimate 16k can
act as"heap" ("heap"). Then
there is an area for stacking, different residencies
tov, etc.  This area can be highlighted
blocks of 256 bytes. I was trying to real-
lyse a multitasking system - something
it worked.  The screenshot can be viewed at
one of AlcoNews. So, time, za-
spent on switching tasks when working
cramped multitasking, not so much
great.  At least less than that
what will be required for shipping, let it be
small program. In its development
I used the scheduler on IM2. And here
if you implement it on NMI, then you can
significantly increase the reliability of the system
we - my implementation does not save in case of cri-
technical errors..."                   
                                        
DWT: NMI - hopeless. I'm already
he said that it is advisable not to touch the iron,
what are we talking about with KilleRamconstant
disputes. I say what we should doOS
under what already really exists (de-
build PROGRAMS for hardware, not on top
mouth), and not once again climb c
soldering iron into an already disfigured
computer.  Moreover, such a rework
will create incompatibility. Regarding the distribution
memory division - it's probably like this
you said it and it will happen. Only under the form-
called by you "bunch", as it seems to me,
1bkb is too much.                         
                                        
 V:"...The main thoughts are as follows:      
                                        
 - make a timer that will
send a signal at regular intervals
NMI  per processor (ideal 0.1/1 second -
You can measure the exact time - at 0.01
most of the time will be spent on
switching tasks, although this is just
twice as fast as standardinterrupt-
niy);                                   
                                        
 - make a port that will be controlled
The operation of this timer is disabling
NMI and the period of the timer (at the expense of the
rioda one might think);                  
                                        
 - flash a new  ROM instead of 128
BASIC or load into cache;     
                                        
 - on the handler NMI will hang
task scheduler..."                  
                                        
 DWT: The first two points and the fourth are not
I'm commenting because it's my point of view
this matter is clear. And here is the third point -
yes. Moreover, it is necessary that OS work
worked equally both from CACHE, and from
ROM.  V:"- y applications have the ability
use interrupts as you wish
niya, and better than the first kind with pro-
frame vector;                       
                                        
 - when flashing the system in ROM real
the possibility of protecting the kernel from hecah-
authorized access from the malicious
nal programs;                           
                                        
 - direct access to ports is possible VG
(or maybe not - I don’t know exactly who
special - enlighten)"                     
                                        
 DWT: Right, right.                     
                                        
V:"- two types can be implemented
preemptive multitasking (non-preemptive
it requires a special approach to writing
programs): allocation of a time slice
_each_ process in turn, the length of each
which dependson process priority;
or a higher priority application simply
will work more often..."                 
                                        
 DWT: Displacement - quite difficult.
Here KilleRam was made by a simple manager
tasks on im2, switching processes, and
thus creating the illusion of parallel
ty work (used some 512b-in-
trifles, like tasks).  Naturally, everything
It was terribly slow. And it is unlikely that such a vi-
Yes, multitasking has a future
Spectrum, if you don’t go into the iron, of course
lezo.                                   
                                      
 V: "...In the version that I propose
I guess, a minimum of hardware modifications -
just a timer and a little logic..."      
                                        
 DWT: There must be no
hardware improvements (in my opinion).   
                                        
 V: "...Of course, this option is needed
is given in detailelaboration, so
that any sentences (except "and nah%%
necessary?  ..”, “what kind of nonsense?..” and the like
nyh :) are accepted and carefully discussed
tsya - after all, making an operating system is not in
basic go :)..."                  
                                        
 DWT: What is true is true. It's already five
years withKilleRamlet's fuck - but real
useless than primitive experiments
mental procedures, no.    
                                        
 V:"...Next, you need to define
system type - separate kernel, separate
graphical shell (as in UNIX) or
unbreakable connection (as inWindows). Only
no need to agree without looking now
to the first option only because
"Windows - motherfucker!"  The shovel is sufficient
there is definitely a lot of information on this issue
cy, I found out that y each method has
their advantages and disadvantages. If anyone
interesting, I can enlighten you popularly -
maybe they’ll give you some new ideas :)..."  DWT: In general, of course, on the one hand
us, Unix-method requires less memory
for the bare core, but "building up" this
kernels can non-centrally generate
another wave of incompatibility, and
programs, it seems to me, will be
shany" with the extra burden of additional drivers
bepob, a bunch of subroutines for implementing
tions of the graphical shell, etc. However
there is a way out - together with theOSdistribution
take a set of standard drivers and pro-
procedures for implementing graphical (or
any other) type of interface.
However, this may cause costs
extra cycles and, as a result, brake
tediousness of the programs and the OS itself. If st-
build a system according to type Windows, then this
method will undoubtedly entail more
higher memory consumption, which when placing
OS in ROM is not desirable. After all, savingROM
will cause initial sluggishness for many
procedures. Exit? Cross these two
dog! That is, some are very smart
and are unlikely to be optimized according to
program-subroutine speedsize
write to ROM, and do the rest
"removable".  That is, the elementary dial
gov mode (regarding the interface) without
all sorts of drivers and additional pro- cesses
stupid - initially, that it will be possible to "replace-
thread" using standard commandsOS (prop-
data, for example, in the starter "package"
"nom" file (as an option - start.run)).  
                                        
 V: "...Finally, you need to get your bearings
into other languages for the OS, not just accem-
Blair.  You can, for example, implement C. He
closest to ASMA, and articles on
this topic is not a problem to find. At the same time re-
sew the format of the procedures..."         
                                        
 DWT:  C - unlikely. Personally, I this language
I hate it and don’t see it (a tautology! :))
promising on Spectrum.It’s very
"contrived" - for me it is more difficult than assembly
Lera (probably because I C studied
afterz80 assembler).                  
                                        
 V:"...How to pass parameters - to
registers or via the stack? Second option
more systematized, but on ZX, from
Unlike PC, there is no such command as push
, so with passing constants
problems may arise in the form of unnecessary
beats  And transmission in registers requires
keep in mind not only the parameters of the fu-
functions, but also in what registers they are
are stored. Also, if the transmission is incorrect,
that parameters through the stack are very likely
dependent.  The solution is to write macros that
some will worry about order themselves
passing parameters.                    
                                        
 Here is an example of the transition from ASMA to C:      
                                        
;type function                          
;void print(byte xpos, byte ypos,      
;byte width, byte size, char* techt)     
 ;сначала опишем макросы                 
macro word ;упаковка двух байт в слово  
dw 256*:1+:0                            
endm                                    
                                        
macro print ;вызов процедуры печати     
db #21 ;ld hl,...                       
word :0,:1                              
db #01 ;ld bc,...                       
word :2,:3                              
ld de,:4                                
push hl                                 
push bc                                 
push de                                 
call print_ ;или                        
endm ;ld а,lib_print_func               
;call sys_lib                           
                                        
...                                     
;где-то в системе                       
print_ рор hl ;адрес возврата           
рор de ;параметры                       
рор bc                                  
ех (sp),hl ;куда вернемся               
... ;тело процедуры                     
ret                                     
                                        
...                                     
                                        
;где-то в приложении                    
print 0.0, 100.60, tech                
...                                     
techt db "Hello, world!",0              
                                      
 Of course, the example is very conventional, but it
shows how transfer can be implemented
chug data through the stack without caring
that you can miss the parameter and the whole system
ma will fly to nirvana - the assembler will give
error already during compilation and you octahet-
Just fix it. All that remains is
standardize the reading order of the para-
meters and output data format..."     
                                        
 DWT: An interesting example and food for ra-
thoughts.  Personally, I thought about the implementation
call system procedures as follows
way.  For example, let RST#10 y us
denotes a group of I/O-commands, then you
The call of the printing procedure is limited to the following
the following set of assembler commands:      
                                        
 rst #10                       
 db 0 ;0th command group -   
 ;subroutine group     
 ;print to screen       
 db 0 ;0th command -          
 ;print in standard  
 ;dialogue mode      
 ;(coordinate depends   
 ;from the cursor position)  
 db "Hello, world!" ;text      
 ;messages  
 db #ff ;marker     
 ;end      
 ;text     
                                        
 Advantages - we do not spoil registers,OS returns program execution immediately
after the end of all arguments. Disadvantage
ki is a rather slow processing system
boots RST#10. However, it is possible to provide
in the system an alternative group of commands,
passing arguments through registers, which
will speed up the process somewhat. Also under-
With the help of such a communication system with OS it is possible
consider vulnerability to errors.       
                                      
 V: “That’s basically all my thoughts.
I am ready to fully cooperate in the creation
research operating systems.                        
                                      
 Article about the Internet navigator. Idea
interesting in itself, but the idea of introducing
defining your own format for storing current
stov using codes - only you -
a game of speed and time. I just
I want to add this. Directly
insert codes into the text, even from
with the power of special programs, not always
convenient.  You can implement the following
riant: writing documents according to the image and
similarity  HTML, i.e.  with all tags and
etc., but with subsequent COMPILATION semi-
valuable text. It also makes sense to
Insert pictures and hyperlinks into
in the form of tables stored separately. Much-
This simplifies text drying and processing
these elements.  It is also possible to implement
store pictures in a packaged fo-
rmate - I’m just writing such a plugin for
BGE.  This uses my method Bit-
Raskwhich was specially designed
for VideoStudio.The benefit from this method is
yes up to 50%  in volume (poorly compressed
only converted pictures, and even then
depending on what) and a loss of about 10%
speed..."                           
                                        
 DWT: Thank you, Vitamin,  for your letter.
KilleRam got crazy about him and in the place,
where we are talking about  Internet navigator
I agreed with you on almost everything.
But this topicit seems to me that it is still
really ghostly. A "bridgehead" is needed for
so-called"Internet navigator", that isOS,
where it can be good "uhterpupo-
"Vat".                                  
                                        
 - - -                  
                                        
I agreed with you on almost everything.
But this topic, it seems to me, is still
really ghostly. A "bridgehead" is needed for
so-called"Internet navigator", that isOS,
where it can be good "uhterpupo-
"Vat".                                  
                                        
 - - -                  
                                        
 Let me finish today
new discussion. We are waiting for your letters with new ones
ideas and suggestions.                 
                                        
 Soon (probably next)
next issue), I will try to preparesome “final” material dedicated to
OS,  in which will be collected, generalized and
all ideas on this topic have been re-analyzed
topic ever heard on the page
tsakh  ZXTime.  Actually, it will be stated
concept of a new OS.                     

Share your thoughts about the article