Adding file io

This commit is contained in:
Christophe Meneboeuf 2016-10-22 01:44:43 +02:00
parent 18f22dec78
commit db68e1bba3
6 changed files with 322 additions and 4 deletions

2
.gitignore vendored Normal file → Executable file
View File

@ -2,6 +2,4 @@
*.a2
*.o
*.map
*.dsk
*.bak
gol

2
Makefile Normal file → Executable file
View File

@ -5,7 +5,7 @@ LINUX_OUT := gol.out
APPLE2_CL := $(CC65_HOME)/bin/cl65
APPLE2_CC := $(CC65_HOME)/bin/cc65
APPLE2_SRC := gol_apple2.c gol_apple2_optimized.asm gfx.asm rnd_colors.asm
APPLE2_SRC := gol_apple2.c gol_apple2_optimized.asm gfx.asm rnd_colors.asm file_io.c mli.asm
APPLE2_MAP := gol_apple2.map
APPLE2_CFLAGS := -Oirs -v -t apple2
APPLE2_OUT := gol.a2

229
file_io.c Executable file
View File

@ -0,0 +1,229 @@
/* Conway's Game of Life for Apple II
* Copyright (C) 2016 Christophe Meneboeuf <christophe@xtof.info>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* Standard headers */
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
/* My Headers */
#include "file_io.h"
uint8_t call_to_mli( uint8_t call, void* ptr_parameter );
#define CREATE 0xC0
#define OPEN 0xC8
#define READ 0xCA
#define WRITE 0xCB
#define CLOSE 0xCC
#define FLUSH 0xCD
#define SET_MARK 0xCE
const char* VOLUME_NAME = "/PRODOS.2.4.1/";
char Filename[ 65 ];
static uint8_t Io[ 1024+256 ]; //I need an adress aligned on 256: I'll use the 1st one inside Io
static uint8_t Error = 0u;
char* get_full_path( const char* p_filename )
{
uint8_t len = strlen(VOLUME_NAME);
if( len > 64u ) {
return 0;
}
Filename[0] = len + strlen(p_filename);
strcpy( 1 + Filename, VOLUME_NAME );
strcpy( 1 + Filename + len, p_filename );
return Filename;
}
/**************************/
typedef struct {
uint8_t param_count;
char* pathname;
uint8_t access;
uint8_t file_type;
uint16_t aux_type;
uint8_t storage_type;
uint16_t create_date;
uint16_t create_time;
} create_param_t;
void file_create( char* p_filename )
{
create_param_t param;
char* filename = get_full_path( p_filename );
if( filename == 0 ) {
Error = BAD_NAME;
return;
}
param.param_count = 7u;
param.pathname = filename;
param.access = 0xE3;
param.file_type = 0x4;
param.aux_type = 0;
param.storage_type = 1;
param.create_date = (78<<8) | (04<<4) | 10;
param.create_time = (12<<8) | 0;
Error = call_to_mli( CREATE, &param );
return;
}
/*****************************/
typedef struct {
uint8_t param_count;
char* pathname;
uint8_t* io_buffer;
uint8_t ref_num;
} open_param_t;
void file_open( char* p_filename, uint8_t* ref_num )
{
open_param_t param;
char* filename = get_full_path( p_filename );
if( filename == 0 ) {
Error = OPEN_ERROR;
return;
}
param.param_count = 3u;
param.pathname = filename;
param.io_buffer = (uint8_t*)((uint16_t)(Io)+256-(uint16_t)(Io)%256); //Getting the 256byte aligned adrress
Error = call_to_mli( OPEN, &param );
if( Error == 0x44 || Error == 0x46 ) {
file_create( p_filename );
if( Error == NO_ERROR ) {
Error = call_to_mli( OPEN, &param );
}
}
if( Error == 0 ) {
*ref_num = param.ref_num;
}
return;
}
/*****************************/
typedef struct {
uint8_t param_count;
uint8_t ref_num;
uint16_t position;
uint8_t dummy; /* high byte always 0 */
} set_mark_param_t;
void file_set_offset( const uint8_t handle, const uint16_t offset )
{
set_mark_param_t param;
param.param_count = 2u;
param.ref_num = handle;
param.position = offset;
param.dummy = 0;
Error = call_to_mli( SET_MARK, &param );
}
/********************************/
typedef struct {
uint8_t param_count;
uint8_t ref_num;
uint8_t* data_buffer;
uint16_t request_count;
uint16_t trans_count;
} read_param_t;
uint16_t file_read( const uint8_t handle, uint8_t* buffer, const uint16_t len )
{
read_param_t param;
param.param_count = 4u;
param.ref_num = handle;
param.data_buffer = buffer;
param.request_count = len;
Error = call_to_mli( READ, &param );
return param.trans_count;
}
/********************************/
typedef struct {
uint8_t param_count;
uint8_t ref_num;
uint8_t* data_buffer;
uint16_t request_count;
uint16_t trans_count;
} write_param_t;
uint16_t file_write( const uint8_t handle, uint8_t* buffer, const uint16_t len )
{
write_param_t param;
param.param_count = 4u;
param.ref_num = handle;
param.data_buffer = buffer;
param.request_count = len;
Error = call_to_mli( WRITE, &param );
return param.trans_count;
}
/*************************/
typedef struct {
uint8_t param_count;
uint8_t ref_num;
} close_param_t;
void file_close( const uint8_t handle )
{
close_param_t param;
param.param_count = 1u;
param.ref_num = handle;
Error = call_to_mli( CLOSE, &param );
}
/*************************/
typedef struct {
uint8_t param_count;
uint8_t ref_num;
} flush_param_t;
void file_flush( const uint8_t handle )
{
flush_param_t param;
param.param_count = 1u;
param.ref_num = handle;
Error = call_to_mli( FLUSH, &param );
}
/*************************/
uint8_t file_error( void )
{
return Error;
}

41
file_io.h Executable file
View File

@ -0,0 +1,41 @@
/* Conway's Game of Life for Apple II
* Copyright (C) 2016 Christophe Meneboeuf <christophe@xtof.info>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __FILE_IO_H__
#define __FILE_IO_H__
#include <stdint.h>
void file_create( char* p_filename );
void file_open( char* p_filename, uint8_t* handle ); /* returns a handle to the file or OPEN_ERROR error code */
void file_set_offset( const uint8_t handle, const uint16_t offset );
uint16_t file_read( const uint8_t handle, uint8_t* out_buffer, const uint16_t len ); /* returns the nb bytes read */
uint16_t file_write( const uint8_t handle, uint8_t* in_buffer, const uint16_t len ); /* returns the nb bytes written */
void file_flush( const uint8_t handle );
void file_close( const uint8_t handle );
uint8_t file_error( void ); /* returns the last file error */
enum {
NO_ERROR = 0u,
FILE_OPENED = 2u,
BAD_NAME,
BAD_LENGTH = 4u,
OPEN_ERROR = 0xFF
};
#endif

2
gol_linux.c Normal file → Executable file
View File

@ -1,4 +1,4 @@
/* Conway's Game of Life for Apple II
/* A Conway's Game of Life
* Copyright (C) 2016 Christophe Meneboeuf <christophe@xtof.info>
*
* This program is free software: you can redistribute it and/or modify

50
mli.asm Executable file
View File

@ -0,0 +1,50 @@
; Conway's Game of Life for Apple II
; Copyright (C) 2016 Christophe Meneboeuf <christophe@xtof.info>
;
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program. If not, see <http://www.gnu.org/licenses/>.
;
; Handling calls to Apple ProDOS 8 MLI
;
.import __dos_type
.import popa
.export _call_to_mli
.code
_call_to_mli:
; Store parameters
STA Mli_Param
STX Mli_Param+1
; Store call number
JSR popa
STA Mli_Call
;+ DEBUG
; CMP #$CE
; BNE continue
;debug: JMP debug
;continue:
;- DEBUG
; Call MLI and return
JSR $BF00 ; MLI call entry point
Mli_Call:
.byte $00
Mli_Param:
.addr $00
RTS
.code