Оберон-клуб «ВЄДАsoft»
https://zx.oberon.org/forum/

pointers on spectrum
https://zx.oberon.org/forum/viewtopic.php?f=10&t=121
Страница 1 из 2

Автор:  slenkar [ 16 июн 2013, 02:16 ]
Заголовок сообщения:  pointers on spectrum

this doesnt seem to compile

It gets translated but then SDCC complains
Obj/hi.c:26: error 101: too many parameters

which is:
__NEW(myptr, gamesquare__2);

Код: "OBERON"
  1. MODULE hi;
  2. IMPORT B := Basic;
  3.  
  4. PROCEDURE Main* ;
  5. TYPE
  6. squarepointer=POINTER TO gamesquare;
  7. gamesquare=RECORD
  8. hcost:SHORTINT
  9. END;
  10.  
  11. VAR
  12. x,y:INTEGER;
  13. gameboard:ARRAY 32,22 OF SHORTINT;
  14. myptr:squarepointer;
  15.  
  16.  
  17. BEGIN B.Init;
  18. NEW (myptr);
  19. myptr.hcost:=23;
  20. gameboard[0][0]:=1;
  21. gameboard[31][21]:=1;
  22. gameboard[14][14]:=1;
  23.  
  24. B.BORDER(B.Green);
  25. B.PAPER(B.Black);
  26. B.CLS;
  27. (*
  28. FOR x:= 0 TO 31 BY 1 DO
  29. FOR y:= 0 TO 21 BY 1 DO
  30. B.AT(y,x);
  31.  
  32. IF gameboard[x][y]=1 THEN
  33. B.PRSTR("y");
  34. ELSE
  35. B.PRSTR("n");
  36. END;
  37. END;
  38. END;
  39. *)
  40. B.PAUSE(B.WaitAKey);
  41. B.Quit;
  42.  
  43. END Main;
  44.  
  45. END hi.
  46.  

Автор:  Zorko [ 16 июн 2013, 17:32 ]
Заголовок сообщения:  Re: pointers on spectrum

Yes, the tagged Oberon pointers and a garbage collector is not implemented for Z80 CPU. If you have an idea how to do it, please tell me. :)

Instead of this just try to use manual memory allocating/deallocating and untagged pointers:
Код: "OBERON"
  1.  
  2. VAR
  3. ptr: POINTER TO ARRAY OF CHAR;
  4. BEGIN
  5. NEW(ptr, 12); (* Allocate memory. *)
  6. COPY("Hello World", ptr^);
  7. C.WriteStr(ptr^);
  8. Platform.DISPOSE(ptr); (* Deallocate memory. *)
  9. ...
Here we need to implement Platform.Dispose procedure:
Код: "OBERON"
  1. (* In module Platform: *)
  2. PROCEDURE -free (memblock: SYSTEM.PTR) "free(memblock)";
  3.  
  4. PROCEDURE DISPOSE* (mem: SYSTEM.PTR);
  5. BEGIN
  6. free(mem);
  7. END DISPOSE;

To implement NEW we need to write SYSTEM_NEWREC and SYSTEM_NEWARR that use low-level procedure SYSTEM_NEWBLK that can be implemented, for example, so:
Код: "C"
/*--------------------------------- Cut here ---------------------------------*/
SYSTEM_PTR SYSTEM_NEWBLK (LONGINT size)
{
SYSTEM_PTR new = SYSTEM_malloc(size);
__ASSERT(new != NIL, 0xFF);
return new;
}
In general, for the time being I haven't dealt with this ​​work line.

Автор:  slenkar [ 16 июн 2013, 21:08 ]
Заголовок сообщения:  Re: pointers on spectrum

aha ok thanks
to allocate a record I do NEW(ptr,sizeof(record)) ?

Just out of curiosity....
How does OFRONT know not to put garbage collection into the spectrum?
does Oberon use the garbage collection of the system? (system.h?)

Автор:  Zorko [ 16 июн 2013, 21:50 ]
Заголовок сообщения:  Re: pointers on spectrum

slenkar писал(а):
to allocate a record I do NEW(ptr,sizeof(record)) ?
Код: "OBERON"
  1. VAR recPtr = POINTER TO RECORD ... END;
  2. BEGIN NEW(recPtr);

slenkar писал(а):
How does OFRONT know not to put garbage collection into the spectrum?
does Oberon use the garbage collection of the system? (system.h?)
Ofront collects the garbage not in each moment, and only if you allocate memory, the runtime search for free blocks and try to re-use its.

If you don't use NEW and pointers at all in your Spectrum program, or use only untagged pointers (Ptr = POINTER [1] TO ...) with manual allocating, garbage collection do not invoked.

Perhaps we would be able to implement a simple garbage collection for Z80 and ZX Spectrum 48K too, and maybe even a dynamic modularity with loading/unloading packed modules from tape, TR DOS disk or 128K memory pages, but we need to think seriously about develop the Oberon-runtime kernel for Z80.

Автор:  slenkar [ 05 июл 2013, 21:59 ]
Заголовок сообщения:  Re: pointers on spectrum

so records cannot be created and arrays cant be resized?

I would like to keep track of what square is in what room to create corridors.
But after the map is created I dont need this information anymore so I can free the memory to hold other information like monsters.

Автор:  Zorko [ 05 июл 2013, 22:07 ]
Заголовок сообщения:  Re: pointers on spectrum

slenkar писал(а):
so records cannot be created?
You can declare and use non-dynamic records:
Код: "OBERON"
  1. VAR Rec = RECORD
  2. field1: INTEGER;
  3. field2: CHAR;
  4. ...
  5. fieldN: BOOLEAN;
  6. END;
How to get a pointer on the record? SYSTEM.ADR(Rec)
But it is a non-Oberon pointer, and internal, for system usage.

slenkar писал(а):
but arrays can be resized?
Non-dynamic arrays cannot be resized. Only dynamic.

Yes, I know - we need to have a simple way to use dynamic records and arrays. Do you know how SDCC's memory manager works with malloc/free?

Автор:  slenkar [ 06 июл 2013, 01:23 ]
Заголовок сообщения:  Re: pointers on spectrum

no, I dont know much about C or C++ sorry

Автор:  Zorko [ 06 июл 2013, 09:19 ]
Заголовок сообщения:  Re: pointers on spectrum

I think everyone who uses Linux knows all about C/C++ ;)

Well, give me a few days, I'll try to develop this work line.

Автор:  slenkar [ 06 июл 2013, 17:39 ]
Заголовок сообщения:  Re: pointers on spectrum

ok I know a tiny amount :D
just how to compile C and some logic

Автор:  Zorko [ 14 июл 2013, 22:41 ]
Заголовок сообщения:  Re: pointers on spectrum

Probably the best C coder for us is Ofront. :) And we can work in simple and fine-designed Oberon.

slenkar, I remember my promise, and this code must work for Speccy too, but I have some problems with malloc under ZX:
Код: "C"
#include <malloc.h>
#include "Basic.h" // Used only for print to screen
 
typedef
struct Person *PersonPtr;
typedef
struct Person {
CHAR name[20];
INTEGER age;
CHAR sex;
PersonPtr next;
} Person;
 
static PersonPtr john, mike, lisa, bob, iren;
 
int main(int argc, char **argv)
{
Basic_Init();
 
john = malloc(sizeof(Person));
if (john==NULL)
{PRSTR((char*)"john=NULL ");}
else
{PRSTR((char*)"john=OK ");}
 
mike = malloc(sizeof(Person));
if (mike==NULL)
{PRSTR((char*)"mike=NULL ");}
else
{PRSTR((char*)"mike=OK ");}
 
lisa = malloc(sizeof(Person));
if (lisa==NULL)
{PRSTR((char*)"lisa=NULL ");}
else
{PRSTR((char*)"lisa=OK ");}
 
bob = malloc(sizeof(Person));
if (bob==NULL)
{PRSTR((char*)"bob=NULL ");}
else
{PRSTR((char*)"bob=OK ");}
 
iren = malloc(sizeof(Person));
if (iren==NULL)
{PRSTR((char*)"iren=NULL ");}
else
{PRSTR((char*)"iren=OK ");}
 
Basic_Quit();
return 0;
}
Here malloc returns a piece of memory first two times, but after it, malloc returns only 0, although the default size of heap is 1 Kb - that should be enough.

Вложения:
DynMem.png
DynMem.png [ 13.87 КБ | Просмотров: 32605 ]

Страница 1 из 2 Часовой пояс: UTC + 2 часа
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/