ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
sys.c
Go to the documentation of this file.
1/*****************************************************************************
2 * ugBASIC - an isomorphic BASIC language compiler for retrocomputers *
3 *****************************************************************************
4 * Copyright 2021-2026 Marco Spedaletti (asimov@mclink.it)
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *----------------------------------------------------------------------------
18 * Concesso in licenza secondo i termini della Licenza Apache, versione 2.0
19 * (la "Licenza"); è proibito usare questo file se non in conformità alla
20 * Licenza. Una copia della Licenza è disponibile all'indirizzo:
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Se non richiesto dalla legislazione vigente o concordato per iscritto,
25 * il software distribuito nei termini della Licenza è distribuito
26 * "COSÌ COM'È", SENZA GARANZIE O CONDIZIONI DI ALCUN TIPO, esplicite o
27 * implicite. Consultare la Licenza per il testo specifico che regola le
28 * autorizzazioni e le limitazioni previste dalla medesima.
29 ****************************************************************************/
30
31/****************************************************************************
32 * INCLUDE SECTION
33 ****************************************************************************/
34
35#include "../../ugbc.h"
36
37/****************************************************************************
38 * CODE SECTION
39 ****************************************************************************/
40
47/* <usermanual>
48@keyword SYS
49
50@english
51This command allows you to start the execution of a subroutine, written directly
52in machine language, starting from the indicated address. It must be noted that
53this jump is intended as a return: any assembly instruction that returns from
54execution will continue the execution of the program from the next ugBASIC line.
55
56Moreover, it is possible to communicate with the machine code. This is made
57possible by indicating, at the same time as the call, the population of specific
58input registers and the recovery of values from specific output registers.
59
60The extended syntax allow the specification of ''r1'', ''r2'', .. as the various
61processor registers, ''v1'', ''v2'', .. are the values passed in the various
62registers and ''x1'', ''x2'', .. are the variables that will receive the
63execution result from the various registers. Since the registers are different
64from CPU to CPU, it can be useful to add the ''ON'' target specification.
65
66@italian
67Questo comando permette di iniziare l'esecuzione di una subroutine, scritta
68direttamente in linguaggio macchina, a partire dall'indirizzo indicato. Bisogna
69fare attenzione che tale salto si intende con ritorno: una eventuale istruzione
70assembly che fa ritornare dall'esecuzione farà continuare l'esecuzione del
71programma dalla riga ugBASIC successiva.
72
73Inoltre è possibile comunicare con il codice macchina. Ciò è reso possibile
74indicando, contestualmente alla chiamata, il popolamento di specifici registri
75di ingresso ed il recupero dei valori da specifici registri di uscita.
76
77La sintassi estesa permette di specificare ''r1'', ''r2'', .. come i vari
78registri del processore, ''v1'', ''v2'', .. sono i valori passati nei vari
79registri e ''x1'', ''x2'', .. sono le variabili che riceveranno l'esito
80dell'esecuzione dai vari registri. Poiché i registri sono diversi da CPU a CPU,
81può essere utile aggiungere la specifica del target ''ON''.
82
83@syntax SYS address _
84@syntax [ WITH REG(r1)=v1[, REG(r2)=v2 [, ... ] ] ] _
85@syntax [ RETURN x1=REG(r1)[, x2=REG(r2)[, ... ] ] ] _
86@syntax [ ON target1[, target2[, ... ] ] ]
87
88@example SYS #49142
89@example SYS indirizzo
90@example SYS indirizzo WITH REG(A)=42 RETURN y=REG(B) ON CPUZ80
91
92@usedInExample extern_example_01.bas
93
94@seeAlso EXEC
95
96@target all
97</usermanual> */
98
99/* <usermanual>
100@keyword EXEC
101
102@english
103This command allows you to start the execution of a subroutine, written directly
104in machine language, starting from the indicated address. It must be noted that
105this jump is intended as a return: any assembly instruction that returns from
106execution will continue the execution of the program from the next ugBASIC line.
107
108Moreover, it is possible to communicate with the machine code. This is made
109possible by indicating, at the same time as the call, the population of specific
110input registers and the recovery of values from specific output registers.
111
112The extended syntax allow the specification of ''r1'', ''r2'', .. as the various
113processor registers, ''v1'', ''v2'', .. are the values passed in the various
114registers and ''x1'', ''x2'', .. are the variables that will receive the
115execution result from the various registers. Since the registers are different
116from CPU to CPU, it can be useful to add the ''ON'' target specification.
117
118@italian
119Questo comando permette di iniziare l'esecuzione di una subroutine, scritta
120direttamente in linguaggio macchina, a partire dall'indirizzo indicato. Bisogna
121fare attenzione che tale salto si intende con ritorno: una eventuale istruzione
122assembly che fa ritornare dall'esecuzione farà continuare l'esecuzione del
123programma dalla riga ugBASIC successiva.
124
125Inoltre è possibile comunicare con il codice macchina. Ciò è reso possibile
126indicando, contestualmente alla chiamata, il popolamento di specifici registri
127di ingresso ed il recupero dei valori da specifici registri di uscita.
128
129La sintassi estesa permette di specificare ''r1'', ''r2'', .. come i vari
130registri del processore, ''v1'', ''v2'', .. sono i valori passati nei vari
131registri e ''x1'', ''x2'', .. sono le variabili che riceveranno l'esito
132dell'esecuzione dai vari registri. Poiché i registri sono diversi da CPU a CPU,
133può essere utile aggiungere la specifica del target ''ON''.
134
135@syntax EXEC address _
136@syntax [ WITH REG(r1)=v1[, REG(r2)=v2 [, ... ] ] ] _
137@syntax [ RETURN x1=REG(r1)[, x2=REG(r2)[, ... ] ] ] _
138@syntax [ ON target1[, target2[, ... ] ] ]
139
140@example EXEC #49142
141@example EXEC indirizzo
142@example EXEC indirizzo WITH REG(A)=42 RETURN y=REG(B) ON CPUZ80
143
144@usedInExample extern_example_01.bas
145
146@seeAlso SYS
147
148@target all
149</usermanual> */
150void sys( Environment * _environment, int _address ) {
151
152 if ( _environment->parameters ) {
153 for( int i=0; i<_environment->parameters; ++i ) {
154 if ( _environment->parametersEach[i] ) {
155 Variable * var = variable_retrieve( _environment, _environment->parametersEach[i] );
156 cpu_set_asmio_indirect( _environment, _environment->parametersAsmioEach[i], var->realName );
157 } else {
158 cpu_set_asmio( _environment, _environment->parametersAsmioEach[i], _environment->parametersValueEach[i] );
159 }
160 }
161 }
162
163 cpu_call_addr( _environment, _address );
164
165 if ( _environment->returns ) {
166 for( int i=0; i<_environment->returns; ++i ) {
167 if ( _environment->returnsEach[i] ) {
168 Variable * var = variable_retrieve( _environment, _environment->returnsEach[i] );
169 cpu_get_asmio_indirect( _environment, _environment->returnsAsmioEach[i], var->realName );
170 }
171 }
172 }
173
174}
175
176void sys_var( Environment * _environment, char * _address ) {
177
178 Variable * address = variable_retrieve_or_define( _environment, _address, VT_ADDRESS, 0 );
179
180 if ( _environment->parameters ) {
181 for( int i=0; i<_environment->parameters; ++i ) {
182 if ( _environment->parametersEach[i] ) {
183 Variable * var = variable_retrieve( _environment, _environment->parametersEach[i] );
184 cpu_set_asmio_indirect( _environment, _environment->parametersAsmioEach[i], var->realName );
185 } else {
186 cpu_set_asmio( _environment, _environment->parametersAsmioEach[i], _environment->parametersValueEach[i] );
187 }
188 }
189 }
190
191 cpu_call_indirect( _environment, address->realName );
192
193 if ( _environment->returns ) {
194 for( int i=0; i<_environment->returns; ++i ) {
195 if ( _environment->returnsEach[_environment->returns-i-1] ) {
196 Variable * var = variable_retrieve( _environment, _environment->returnsEach[_environment->returns-i-1] );
197 cpu_get_asmio_indirect( _environment, _environment->returnsAsmioEach[_environment->returns-i-1], var->realName );
198 }
199 }
200 }
201
202}
void cpu_set_asmio_indirect(Environment *_environment, int _asmio, char *_value)
Definition 6309.c:3904
void cpu_get_asmio_indirect(Environment *_environment, int _asmio, char *_value)
Definition 6309.c:3967
void cpu_call_indirect(Environment *_environment, char *_value)
Definition 6309.c:3765
void cpu_set_asmio(Environment *_environment, int _asmio, int _value)
Definition 6309.c:3838
void cpu_call_addr(Environment *_environment, int _address)
Definition 6309.c:3749
Variable * variable_retrieve(Environment *_environment, char *_name)
Variable * variable_retrieve_or_define(Environment *_environment, char *_name, VariableType _type, int _value)
char * parametersEach[MAX_PARAMETERS]
Definition ugbc.h:2790
int returnsAsmioEach[MAX_PARAMETERS]
Definition ugbc.h:2820
int parameters
Definition ugbc.h:2785
int returns
Definition ugbc.h:2810
int parametersValueEach[MAX_PARAMETERS]
Definition ugbc.h:2805
int parametersAsmioEach[MAX_PARAMETERS]
Definition ugbc.h:2795
char * returnsEach[MAX_PARAMETERS]
Definition ugbc.h:2815
char * realName
Definition ugbc.h:982
void sys(Environment *_environment, int _address)
Emit code for SYS / EXEC ... command.
Definition sys.c:150
void sys_var(Environment *_environment, char *_address)
Definition sys.c:176
struct _Variable Variable
Structure of a single variable.
struct _Environment Environment
Structure of compilation environment.
@ VT_ADDRESS
Definition ugbc.h:465