ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
wait_cycles.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
49void wait_cycles( Environment * _environment, int _timing, int _parallel ) {
50
51 if ( _environment->protothread && _environment->procedureName && _parallel ) {
52
53 if ( _environment->protothreadForbid ) {
55 }
56
57 char waitVariableName[MAX_TEMPORARY_STORAGE]; sprintf(waitVariableName, "%swaitms%d", _environment->procedureName, _environment->protothreadStep );
58
60 memset( ((struct _Environment *)_environment)->arrayDimensionsEach, 0, sizeof( int ) * MAX_ARRAY_DIMENSIONS );
61 ((struct _Environment *)_environment)->arrayDimensionsEach[0] = _environment->protothreadConfig.count;
62 ((struct _Environment *)_environment)->arrayDimensions = 1;
63 variable_define( _environment, waitVariableName, VT_TARRAY, 0 );
64 variable_array_type( _environment, waitVariableName, VT_WORD );
66
67 variable_store_array_const( _environment, waitVariableName, _timing );
68
69 yield( _environment );
70
71 char protothreadLabel[MAX_TEMPORARY_STORAGE]; sprintf(protothreadLabel, "%spt%d", _environment->procedureName, _environment->protothreadStep );
72
73 variable_decrement_mt( _environment, waitVariableName );
74 Variable * result = variable_temporary( _environment, VT_WORD, "(temporary)" );
75 variable_move_from_mt( _environment, waitVariableName, result->name );
76 variable_compare_and_branch_const( _environment, result->name, 0, protothreadLabel, 1 );
77
78 cpu_protothread_save( _environment, "PROTOTHREADCT", ( _environment->protothreadStep - 1 ) );
79 cpu_protothread_set_state( _environment, "PROTOTHREADCT", PROTOTHREAD_STATUS_YIELDED );
80 cpu_return( _environment );
81
82 cpu_label( _environment, protothreadLabel );
83 cpu_protothread_set_state( _environment, "PROTOTHREADCT", PROTOTHREAD_STATUS_RUNNING );
84
85 ++_environment->protothreadStep;
86
87 } else {
88
89 char timingString[MAX_TEMPORARY_STORAGE]; sprintf(timingString, "$%2.2x", _timing );
90
91 cpu_busy_wait( _environment, timingString );
92
93 }
94
95}
96
105void wait_cycles_var( Environment * _environment, char * _timing, int _parallel ) {
106
107 Variable * timing = variable_retrieve( _environment, _timing );
108
109 if ( _environment->protothread && _environment->procedureName && _parallel ) {
110
111 char waitVariableName[MAX_TEMPORARY_STORAGE]; sprintf(waitVariableName, "%swaitms%d", _environment->procedureName, _environment->protothreadStep );
112
114 memset( ((struct _Environment *)_environment)->arrayDimensionsEach, 0, sizeof( int ) * MAX_ARRAY_DIMENSIONS );
115 ((struct _Environment *)_environment)->arrayDimensionsEach[0] = _environment->protothreadConfig.count;
116 ((struct _Environment *)_environment)->arrayDimensions = 1;
117 variable_define( _environment, waitVariableName, VT_TARRAY, 0 );
118 variable_array_type( _environment, waitVariableName, VT_WORD );
120
121 variable_move_to_mt( _environment, _timing, waitVariableName );
122
123 yield( _environment );
124
125 char protothreadLabel[MAX_TEMPORARY_STORAGE]; sprintf(protothreadLabel, "%spt%d", _environment->procedureName, _environment->protothreadStep );
126
127 variable_decrement_mt( _environment, waitVariableName );
128 Variable * result = variable_temporary( _environment, VT_WORD, "(temporary)" );
129 variable_move_from_mt( _environment, waitVariableName, result->name );
130 variable_compare_and_branch_const( _environment, result->name, 0, protothreadLabel, 1 );
131
132 cpu_protothread_save( _environment, "PROTOTHREADCT", ( _environment->protothreadStep - 1 ) );
133 cpu_protothread_set_state( _environment, "PROTOTHREADCT", PROTOTHREAD_STATUS_YIELDED );
134 cpu_return( _environment );
135
136 cpu_label( _environment, protothreadLabel );
137 cpu_protothread_set_state( _environment, "PROTOTHREADCT", PROTOTHREAD_STATUS_RUNNING );
138
139 ++_environment->protothreadStep;
140
141 } else {
142
143 cpu_busy_wait( _environment, timing->realName );
144
145 }
146
147}
void cpu_protothread_save(Environment *_environment, char *_index, int _step)
Definition 6309.c:6203
void cpu_protothread_set_state(Environment *_environment, char *_index, int _state)
Definition 6309.c:6226
void cpu_label(Environment *_environment, char *_label)
Definition 6309.c:356
void cpu_return(Environment *_environment)
Definition 6309.c:4030
void cpu_busy_wait(Environment *_environment, char *_timing)
Definition 6309.c:4183
Variable * variable_retrieve(Environment *_environment, char *_name)
void variable_compare_and_branch_const(Environment *_environment, char *_source, int _destination, char *_name, int _positive)
Variable * variable_array_type(Environment *_environment, char *_name, VariableType _type)
Variable * variable_move_from_mt(Environment *_environment, char *_source, char *_destination)
Increment a variable by one.
void variable_store_array_const(Environment *_environment, char *_array, int _value)
Variable * variable_define(Environment *_environment, char *_name, VariableType _type, int _value)
Define a variable for the program.
Variable * variable_move_to_mt(Environment *_environment, char *_source, char *_destination)
Variable * variable_temporary(Environment *_environment, VariableType _type, char *_meaning)
Define a temporary variable.
void variable_decrement_mt(Environment *_environment, char *_source)
Decrement a variable by one.
void wait_cycles_var(Environment *_environment, char *_timing, int _parallel)
Emit ASM code for WAIT [expression] CYCLES.
void wait_cycles(Environment *_environment, int _timing, int _parallel)
Emit ASM code for WAIT # [integer] CYCLES.
Definition wait_cycles.c:49
Structure of compilation environment.
Definition ugbc.h:2269
int protothread
Definition ugbc.h:2830
int arrayDimensions
Definition ugbc.h:2718
int protothreadStep
Definition ugbc.h:2840
int arrayDimensionsEach[MAX_ARRAY_DIMENSIONS]
Definition ugbc.h:2723
ProtothreadConfig protothreadConfig
Definition ugbc.h:2430
char * procedureName
Definition ugbc.h:2775
int protothreadForbid
Definition ugbc.h:2845
char * name
Definition ugbc.h:979
char * realName
Definition ugbc.h:982
#define MAX_TEMPORARY_STORAGE
Definition ugbc.h:563
#define MAX_ARRAY_DIMENSIONS
Definition ugbc.h:566
struct _Variable Variable
Structure of a single variable.
struct _Environment Environment
Structure of compilation environment.
@ VT_TARRAY
Definition ugbc.h:480
@ VT_WORD
Definition ugbc.h:455
#define PROTOTHREAD_STATUS_YIELDED
Definition ugbc.h:4548
#define CRITICAL_MULTITASKING_FORBIDDEN()
Definition ugbc.h:3703
#define PROTOTHREAD_STATUS_RUNNING
Definition ugbc.h:4547
void yield(Environment *_environment)
Emit code for YIELD.
Definition yield.c:63