-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcpu.cpp
More file actions
300 lines (263 loc) · 7.94 KB
/
cpu.cpp
File metadata and controls
300 lines (263 loc) · 7.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**************************************************************************
* Copyright (C), AirM2M Tech. Co., Ltd.
*
* Name: cpu.cpp
* Author: panjun
* Version: V0.1
* Date: 2016/09/13
*
* Description:
* Module for interfacing with CPU.
* History:
* panjun 16/09/13 Initially create file.
**************************************************************************/
#include "stdafx.h"
#include "lua.hpp"
#include "auxmods.h"
#include "platform.h"
#include "platform_conf.h"
// Lua: w32( address, data )
static int cpu_w32( lua_State *L )
{
UINT32 addr, data;
luaL_checkinteger( L, 1 );
luaL_checkinteger( L, 2 );
addr = ( UINT32 )luaL_checknumber( L, 1 );
data = ( UINT32 )luaL_checknumber( L, 2 );
*(UINT32*)addr = data;
return 0;
}
// Lua: data = r32( address )
static int cpu_r32( lua_State *L )
{
UINT32 addr;
luaL_checkinteger( L, 1 );
addr = ( UINT32 )luaL_checknumber( L, 1 );
lua_pushnumber( L, ( lua_Number )(*( UINT32*)addr ) );
return 1;
}
// Lua: w16( address, data )
static int cpu_w16( lua_State *L )
{
UINT32 addr;
UINT16 data = (UINT16)luaL_checkinteger( L, 2 );
luaL_checkinteger( L, 1 );
addr = (UINT32)luaL_checknumber( L, 1 );
*(UINT16*)addr = data;
return 0;
}
// Lua: data = r16( address )
static int cpu_r16( lua_State *L )
{
UINT32 addr;
luaL_checkinteger( L, 1 );
addr = ( UINT32 )luaL_checknumber( L, 1 );
lua_pushnumber( L, ( lua_Number )(*(UINT16*)addr ));
return 1;
}
// Lua: w8( address, data )
static int cpu_w8( lua_State *L )
{
UINT32 addr;
UINT8 data = (UINT8)luaL_checkinteger( L, 2 );
luaL_checkinteger( L, 1 );
addr = ( UINT32 )luaL_checknumber( L, 1 );
*(UINT8*)addr = data;
return 0;
}
// Lua: data = r8( address )
static int cpu_r8( lua_State *L )
{
UINT32 addr;
luaL_checkinteger( L, 1 );
addr = (UINT32)luaL_checknumber( L, 1 );
lua_pushnumber( L, ( lua_Number )(*(UINT8*)addr));
return 1;
}
// Either disables or enables the given interrupt(s)
static int cpuh_int_helper( lua_State *L, int mode )
{
#ifdef BUILD_LUA_INT_HANDLERS
unsigned i;
elua_int_id id;
elua_int_resnum resnum;
int res;
if( lua_gettop( L ) > 0 )
{
id = ( elua_int_id )luaL_checkinteger( L, 1 );
if( id < ELUA_INT_FIRST_ID || id > INT_ELUA_LAST )
return luaL_error( L, "invalid interrupt ID" );
for( i = 2; i <= lua_gettop( L ); i ++ )
{
resnum = ( elua_int_resnum )luaL_checkinteger( L, i );
res = platform_cpu_set_interrupt( id, resnum, mode );
if( res == PLATFORM_INT_INVALID )
return luaL_error( L, "%d is not a valid interrupt ID", ( int )id );
else if( res == PLATFORM_INT_NOT_HANDLED )
return luaL_error( L, "'%s' not implemented for interrupt %d with resource %d", mode == PLATFORM_CPU_ENABLE ? "sei" : "cli", ( int )id, ( int )resnum );
else if( res == PLATFORM_INT_BAD_RESNUM )
return luaL_error( L, "resource %d not valid for interrupt %d", ( int )resnum, ( int )id );
}
}
else
#else // #ifdef BUILD_LUA_INT_HANDLERS
if( lua_gettop( L ) > 0 )
return luaL_error( L, "Lua interrupt support not available." );
#endif // #ifdef BUILD_LUA_INT_HANDLERS
platform_cpu_set_global_interrupts( mode );
return 0;
}
// Lua: cpu.cli( id, resnum1, [resnum2], ... [resnumn] )
static int cpu_cli( lua_State *L )
{
return cpuh_int_helper( L, PLATFORM_CPU_DISABLE );
}
// Lua: cpu.sei( id, resnum1, [resnum2], ... [resnumn] )
static int cpu_sei( lua_State *L )
{
return cpuh_int_helper( L, PLATFORM_CPU_ENABLE );
}
// Lua: frequency = clock()
static int cpu_clock( lua_State *L )
{
lua_pushinteger(L, platform_cpu_get_frequency());
return 1;
}
// CPU constants list
typedef struct
{
const char* name;
UINT32 val;
} cpu_const_t;
#ifdef PLATFORM_CPU_CONSTANTS
static const cpu_const_t cpu_constants[] =
{
PLATFORM_CPU_CONSTANTS,
{ NULL, 0 }
};
static int cpu_mt_index( lua_State *L )
{
const char *key = luaL_checkstring( L, 2 );
unsigned i = 0;
while( cpu_constants[ i ].name != NULL )
{
if( !strcmp( cpu_constants[ i ].name, key ) )
{
lua_pushnumber( L, cpu_constants[ i ].val );
return 1;
}
i ++;
}
return 0;
}
#endif
#ifdef BUILD_LUA_INT_HANDLERS
// Lua: prevhandler = cpu.set_int_handler( id, f )
static int cpu_set_int_handler( lua_State *L )
{
int id = ( int )luaL_checkinteger( L, 1 );
if( id < ELUA_INT_FIRST_ID || id > INT_ELUA_LAST )
return luaL_error( L, "invalid interrupt ID" );
if( lua_type( L, 2 ) == LUA_TFUNCTION /*|| lua_type( L, 2 ) == LUA_TLIGHTFUNCTION*/ || lua_type( L, 2 ) == LUA_TNIL )
{
if( lua_type( L, 2 ) == LUA_TNIL )
elua_int_disable( id );
else
elua_int_enable( id );
lua_settop( L, 2 ); // id f
lua_rawgeti( L, LUA_REGISTRYINDEX, LUA_INT_HANDLER_KEY ); // id f inttable
lua_rawgeti( L, -1, id ); // id f inttable prevf
lua_replace( L, 1 ); // prevf f inttable
lua_pushvalue( L, 2 ); // prevf f inttable f
lua_rawseti( L, -2, id ); // prevf f inttable
lua_pop( L, 2 ); // prevf
return 1;
}
else
return luaL_error( L, "invalid handler type (must be a function or nil)" );
return 0;
}
// Lua: handler = cpu.get_int_handler( id )
static int cpu_get_int_handler( lua_State *L )
{
int id = ( int )luaL_checkinteger( L, 1 );
if( id < ELUA_INT_FIRST_ID || id > INT_ELUA_LAST )
return luaL_error( L, "invalid interrupt ID" );
lua_rawgeti( L, LUA_REGISTRYINDEX, LUA_INT_HANDLER_KEY );
lua_rawgeti( L, -1, id );
return 1;
}
// Lua: flag = get_int_flag( id, resnum, [clear] )
// 'clear' default to true if not specified
static int cpu_get_int_flag( lua_State *L )
{
elua_int_id id;
elua_int_resnum resnum;
int clear = 1;
int res;
id = ( elua_int_id )luaL_checkinteger( L, 1 );
resnum = ( elua_int_resnum )luaL_checkinteger( L, 2 );
if( lua_gettop( L ) >= 3 )
{
if( lua_isboolean( L, 3 ) )
clear = lua_toboolean( L, 3 );
else
return luaL_error( L, "expected a bool as the 3rd argument of this function" );
}
res = platform_cpu_get_interrupt_flag( id, resnum, clear );
if( res == PLATFORM_INT_INVALID )
return luaL_error( L, "%d is not a valid interrupt ID", ( int )id );
else if( res == PLATFORM_INT_NOT_HANDLED )
return luaL_error( L, "get flag operation not implemented for interrupt %d with resource %d", ( int )id, ( int )resnum );
else if( res == PLATFORM_INT_BAD_RESNUM )
return luaL_error( L, "resource %d not valid for interrupt %d", ( int )resnum, ( int )id );
lua_pushinteger( L, res );
return 1;
}
#endif // #ifdef BUILD_LUA_INT_HANDLERS
// Module function map
#define MIN_OPT_LEVEL 2
const luaL_Reg cpu_map[] =
{
{"w32", cpu_w32},
{"r32", cpu_r32},
{"w16", cpu_w16},
{"r16", cpu_r16},
{"w8", cpu_w8},
{"r8", cpu_r8},
{"cli", cpu_cli},
{"sei", cpu_sei},
{"clock", cpu_clock},
#ifdef BUILD_LUA_INT_HANDLERS
{"set_int_handler", cpu_set_int_handler},
{"get_int_handler", cpu_get_int_handler},
{"get_int_flag", cpu_get_int_flag},
#endif
#if defined( PLATFORM_CPU_CONSTANTS ) && LUA_OPTIMIZE_MEMORY > 0
{"__metatable", cpu_map},
#endif
#ifdef PLATFORM_CPU_CONSTANTS
{"__index", cpu_mt_index},
#endif
{ NULL, NULL }
};
LUALIB_API int luaopen_cpu( lua_State *L )
{
#ifdef BUILD_LUA_INT_HANDLERS
// Create interrupt table
lua_newtable( L );
lua_rawseti( L, LUA_REGISTRYINDEX, LUA_INT_HANDLER_KEY );
#endif //#ifdef BUILD_LUA_INT_HANDLERS
#if LUA_OPTIMIZE_MEMORY > 0
return 0;
#else // #if LUA_OPTIMIZE_MEMORY > 0
// Register methods
luaL_register(L, AUXLIB_CPU, cpu_map);
#ifdef PLATFORM_CPU_CONSTANTS
// Set table as its own metatable
lua_pushvalue(L, -1);
lua_setmetatable(L, -2);
#endif // #ifdef PLATFORM_CPU_CONSTANTS
return 1;
#endif // #if LUA_OPTIMIZE_MEMORY > 0
}