#include "addrs.h"

void init_pools(void);

/* if each pool is 16K, that's 512K, not bad. however, overhead. maybe
   limit this to something like 20 (128K extended = 16, plus the big
   pool in main bank, plus the potential smaller pools at MEMLO and
   $d800. */
#define MAX_POOLS 32

/* with 512K, we get one screen per pool.
   with 256K, up to 2 screens per pool.
   with 128K, up to 4.
   with 64K, we only get 1 large pool.
   with 48K, we only get 1 pool.
*/
// #define MAX_SCREENS 32
// for now anyway:
#define MAX_SCREENS 10

/* if a pool is not in use, its screen_count will be this. */
#define POOL_UNUSED 0xff

/* length of a line in bytes. this will eventually be 80 for
   xep-80 support. */
#define LINE_LEN 40

/* 42 bytes per line */
typedef struct line_s {
  struct line_s *next;
  char data[LINE_LEN];
} line_t;

/* the end marker line is a line_t, but it lives outside of any pool and
   has a 'next' pointer that points to itself. */

/* sizeof(screen_t) is 33 bytes... */
typedef struct {
  char title[25];
  char status;
  char pool; /* this could be a pool_t * instead */
  unsigned int line_count; /* can be above 255 */
  line_t *line_list; /* head of a linked list */
  unsigned int scrollback_pos; /* also can be >255 */
} screen_t;

extern screen_t screens[MAX_SCREENS]; /* array is 1023 bytes */

typedef struct {
  // I don't think we even need these:
  // u16 start; /* 0 = not in use */
  // u16 end;
  char screen_count; /* maybe set to $ff for "not in use" */
  /* int line_count; // maybe better to use line count instead of
                        screen count to decide which pool to create
                        new screens in? */
  char bank; /* probably this is just the PORTB value */
  line_t *free_list; /* when this is null, the pool has no free lines */
} pool_t;

/* this array is sizeof(pool) * 9, so 288 bytes */
/* the code that builds this array (detects extended ram too),
   will live in the config segment. */
extern pool_t pools[MAX_POOLS];

/* this function is responsible for counting the lines and arranging
   them in a linked list. it zeroes the 40 character bytes in each
   line and sets the other 2 as a pointer to the next. */
void add_to_pool(char p, u16 start, u16 end);

/* returns the index into pools[] of the pool with the smallest
   screen_count. */
char get_smallest_pool(void);

/* add a line to a screen */
void add_line(char s);
